Skip to content

Instantly share code, notes, and snippets.

@tlaitinen
Created February 13, 2019 11:54
Show Gist options
  • Save tlaitinen/82668ec39b4cbd9eeb907598a36267ca to your computer and use it in GitHub Desktop.
Save tlaitinen/82668ec39b4cbd9eeb907598a36267ca to your computer and use it in GitHub Desktop.
import React from 'react';
import {
typedConnect,
createPropsMapper,
PropsOf
} from 'react-redux-typed-connect';
export function makeTestComponent<E,RS>(getEntity:(state:RS, id:string) => E | undefined) {
type Props = PropsOf<typeof propsMapper>;
const TestComponent = (props:Props) => (
<div>
{JSON.stringify(props.entity)}
</div>
);
const propsMapper = createPropsMapper({
fromState: (state:RS, ownProps:{id?:string}) => ({
entity: ownProps.id ? getEntity(state, ownProps.id) : undefined
}),
actions: () => ({})
});
return typedConnect(propsMapper)(TestComponent);
}
interface Dummy {
id: string;
dummy: string;
}
interface RootState {
dummies: {
[id:string]: Dummy | undefined
};
}
function getDummy(state:RootState, id:string) {
return state.dummies[id];
}
export const tc = makeTestComponent(getDummy);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment