Skip to content

Instantly share code, notes, and snippets.

View mikebridge's full-sized avatar
🤔
Thinking

Mike Bridge mikebridge

🤔
Thinking
  • M Bridge Canada Consulting Ltd.
  • Edmonton, Canada
  • 01:22 (UTC -06:00)
View GitHub Profile
@mikebridge
mikebridge / withQueryString.tsx
Created May 16, 2017 19:17
query string HOC for react-router 4
import * as React from "react";
type HOCWrapped<PWrapped, PHoc> = React.ComponentClass<PWrapped & PHoc> | React.SFC<PWrapped & PHoc>;
const queryString = require("query-string");
import {RouteComponentProps, withRouter} from "react-router";
export interface IQueryStringProps {
params: any;
@mikebridge
mikebridge / package.json
Created May 15, 2017 21:53
JSON to add to package.json to enable Jest/Typescript debugging in IntelliJ
{
"jest": {
"transform": {
"^.+\\.css$": "react-scripts-ts/config/jest/cssTransform.js",
".(ts|tsx)": "react-scripts-ts/config/jest/typescriptTransform.js",
"^(?!.*\\.(css|json)$)": "react-scripts-ts/config/jest/fileTransform.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
@mikebridge
mikebridge / welcome3.tsx
Created May 5, 2017 02:25
TypeScript control with stacked HOCs
import * as React from "react";
import {IWithPersonalizationProps, withPersonalization} from "./withPersonalization2";
import {IWithNavigationProps, withNavigation} from "./withNavigation";
// This empty declaration is required
interface IWelcomeOwnProps {}
type IWelcomeProps = IWelcomeOwnProps & IWithNavigationProps & IWithPersonalizationProps;
class Welcome extends React.Component<IWelcomeProps, {}> {
@mikebridge
mikebridge / withNavigation.tsx
Last active January 18, 2018 09:01
Example typescript wrapper around react-router 4 for blog entry
import * as React from "react";
import {RouteComponentProps, withRouter} from "react-router";
export interface IWithNavigationProps {
navigate: () => void;
}
export function withNavigation<P, S>(
Component: React.ComponentClass<P & IWithNavigationProps> | React.SFC<P & IWithNavigationProps>,
routeWhenClicked: string): React.ComponentClass<P> {
@mikebridge
mikebridge / withPersonalization2.test.tsx
Last active May 4, 2017 22:59
TypeScript tests for the Redux HOC
import * as React from "react";
import {shallow, mount} from "enzyme";
import configureStore from "redux-mock-store";
import withPersonalization from "./withPersonalization2";
import {IWithPersonalizationProps} from "./withPersonalization2";
import {Provider} from "react-redux";
// test helpers
const middlewares = [];
const mockStore = configureStore(middlewares);
@mikebridge
mikebridge / withPersonalization2.tsx
Created May 4, 2017 22:30
A TypeScript HOC to inject props into a component from Redux
import * as React from "react";
import * as ReactRedux from "react-redux";
export interface IWithPersonalizationProps {
name: string;
}
type HOC<PWrapped, PHoc> = React.ComponentClass<PWrapped & PHoc> | React.SFC<PWrapped & PHoc>;
export function withPersonalization<P, S>(Component: HOC<P, IWithPersonalizationProps>): React.ComponentClass<P> {
import * as React from "react";
import {IWithPersonalizationProps, withPersonalization} from "./withPersonalization";
interface IWelcomeOwnProps {
onClick: () => void;
}
export class Welcome extends React.Component<IWelcomeOwnProps & IWithPersonalizationProps, {}> {
constructor(props: IWelcomeOwnProps & IWithPersonalizationProps) {
@mikebridge
mikebridge / withPersonalization.tsx
Last active May 4, 2017 17:56
First iteration of a Higher Order Function in TypeScript
import * as React from "react";
export interface IWithPersonalizationProps {
name: string;
}
type HOCWrapped<P, PHoc> = React.ComponentClass<P & PHoc> | React.SFC<P & PHoc>;
export function withPersonalization<P, S>(Component: HOCWrapped<P, IWithPersonalizationProps>): React.ComponentClass<P> {
@mikebridge
mikebridge / welcome.test.tsx
Created May 4, 2017 15:56
Enzyme test for welcome.tsx
import * as React from "react";
import {Welcome} from "./welcome";
import {shallow} from "enzyme";
const name = "Tester;
describe("<Welcome />", () => {
it("renders a greeting from the name", () => {
const wrapper = shallow(<Welcome name={name} onClick={() => null} />);
@mikebridge
mikebridge / welcome.tsx
Last active May 4, 2017 15:54
Typescript Welcome-User Component
import * as React from "react";
interface IWelcomeProps {
name: string;
onClick: () => void;
}
export class Welcome extends React.Component<IWelcomeProps, {}> {
constructor(props: IWelcomeProps) {