Created
February 13, 2019 11:54
-
-
Save tlaitinen/82668ec39b4cbd9eeb907598a36267ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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