Skip to content

Instantly share code, notes, and snippets.

View mikebridge's full-sized avatar
🤔
Thinking

Mike Bridge mikebridge

🤔
Thinking
View GitHub Profile
@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) {
@mikebridge
mikebridge / stateless.test.tsx
Created May 1, 2017 17:22
Some typescript tests for stateless components
import * as React from "react";
import {
AnotherSimpleStatelessComponent,
SimpleStatelessComponent,
TerseStatelessComponent,
VerboseStatelessComponent,
ReallyVerboseStatelessComponent} from "./stateless";
import {shallow} from "enzyme";
describe("Stateless Examples", () => {
@mikebridge
mikebridge / exampleComponent.tsx
Created April 28, 2017 20:33
An example react component in TypeScript
import * as React from "react";
import * as PropTypes from "prop-types";
interface IExampleComponentProps {
text?: string;
onCounterIncrease: (count: number) => void;
}
interface IExampleComponentState {
clicks: number;
@mikebridge
mikebridge / tslint.json
Created April 28, 2017 20:26
example tslint.json
{
"extends": ["tslint-react"],
"rules": {
"align": [
true,
"parameters",
"arguments",
"statements"
],
"ban": false,