redux-render-prop
Redux with render props. Alternative to the connect()
higher order component.
Read an introductory blog post here.
Very TypeScript friendly. It heavily leverages type inference to avoid manual typing of props.
Install
For react-redux 6.x
npm install redux-render-prop react-redux@6 # has peer dep of react-redux 6.x
For react-redux 5.x you must use pre 0.7 versions
npm install redux-render-prop@0.6 react-redux@5 # has peer dep of react-redux 5.x
For Typescript you will need the types too
npm install @types/react-dom @types/react @types/react-redux
Usage
;; // Define state as a single type // Define some actions creators; // Create render prop component creator with app specific types.// There is usually only one of these per app; // Create render prop component for counters.; // Must be wrapped with <Provider store={store} />;
Flattening render props
If you find yourself nesting too much you can flatten the render callbacks
type safely with the MappedState
, MappedActions
and
MappedStateAndActions
type helpers like so:
;
You can also use it to pass the props to class components if you need to access the mapped state or actions from lifecycle methods.
;
Memoizing
For advanced high performance you may use memoizeMapState()
to
create memoized selectors on component mount.
;
Examples
Here's a more complete example with immer-reducer: