Skip to content

Commit a2138f6

Browse files
mudrzjosepot
authored andcommitted
Update api.md - factory functions (reduxjs#805)
* Update api.md - factory functions * Update api.md minor fixes * remove all comments
1 parent 8c4bf6f commit a2138f6

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/api.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,39 @@ function mergeProps(stateProps, dispatchProps, ownProps) {
324324
export default connect(mapStateToProps, actionCreators, mergeProps)(TodoApp)
325325
```
326326

327+
##### Factory functions
328+
Factory functions can be used for performance optimizations
329+
330+
```js
331+
import { addTodo } from './actionCreators'
332+
333+
function mapStateToPropsFactory(initialState, initialProps) {
334+
const getSomeProperty= createSelector(...);
335+
const anotherProperty = 200 + initialState[initialProps.another];
336+
return function(state){
337+
return {
338+
anotherProperty,
339+
someProperty: getSomeProperty(state),
340+
todos: state.todos
341+
}
342+
}
343+
}
344+
345+
function mapDispatchToPropsFactory(initialState, initialProps) {
346+
function goToSomeLink(){
347+
initialProps.history.push('some/link');
348+
}
349+
return function(dispatch){
350+
return {
351+
addTodo
352+
}
353+
}
354+
}
355+
356+
357+
export default connect(mapStateToPropsFactory, mapDispatchToPropsFactory)(TodoApp)
358+
```
359+
327360
<a id="connectAdvanced"></a>
328361
### `connectAdvanced(selectorFactory, [connectOptions])`
329362

0 commit comments

Comments
 (0)