Weitere ähnliche Inhalte Ähnlich wie Wrap up Presentation Flux/Redux (20) Kürzlich hochgeladen (20) Wrap up Presentation Flux/Redux2. • Flux
• Redux
• Action creators
• State
• Containers and Selectors
• Middleware
• Perks
• Perf tricks (React & Redux)
GENERAL OVERVIEW
5. • Not a lib
• Not a framework
• No precise guidelines or best practices
• DIY
• But a mainstream winner: redux
FLUX
7. REDUX
• 5 JS functions
• Opinionated flux implementation
• Add-on Ecosystem
• Compatible with other rendering frameworks
(ng, ng2, bb, polymer, ember)
10. SECUNDUM REDUX
STATE
• State of the app (raw data, conformation)
• Managed by a single store
• ℂ handled via composed reducers
• Represented as a litteral Object in JS
• Immutable
• Deep nesting or over ℂ should be banned
DISPATCHER
REDUCER
APP STATE
REDUCER
REDUCER
Event bus
11. STATE
e.g.:
{
players: {
‘46’: {name, seed},
‘21’: {name, seed},
…
}
displayedPlayers: [’56’, ’48’, ‘2’, ’89’],
currentOrdering: ‘seed’,
route: [‘players’, ‘panel’]
}
12. NAMED AFTER THEIR SIGNATURE
REDUCERS
• A reducer is a pure function
• no scope impact
• synchronous
• Signature:
(state, action) -> state
13. REDUCERS
• Reducers can be composed :
const reducer0 = combine({
players: reducer1,
displayedPlayers: reducer2,
currentOrdering: reducer3
…
})
// has a reducer signature
14. REDUCERS
• Reducers can be nested :
// in players reducer
const newState = players.map(
player =>
playerReducer(state(playerId), player)
)
16. Selectors
• are pure functions
• create a custom representation of the app state
// Object(~Map) -> Object(~Map)
({players, displayedPlayers}) => ({
players: displayedPlayers
.map(playerId => players[playerId])
.sort()
})
CONTAINERS/SELECTORS
17. Containers connect state, action creators and view
// state selector
const mapStateToProps = (state) => ({
ordering: state.currentOrdering
})
// action creators picker
const mapDispatchToProps = (dispatch) =>
bindActionCreators({ changeOrdering }, dispatch)
exports = connect(mapStateToProps, mapDispatchToProps)(ReactComp)
CONTAINERS/CONNECT
19. What about AJAX call or Websocket frames ?
What about effects in time ?
What about everything async ?
21. Middleware manage all asynchronous processes
module.exports = store => next => action => {
// do something async
if (action.type === FETCH_PLAYERS_INFO)
fetch(url).then(store.dispatch(updatePlayersInfo))
// handover action to store (reducers)
next(action)
}
MIDDLEWARE
24. PERF TRICKS
• NATIVE requestAnimationFrame
• REACT shouldComponentUpdate
• REACT pureRenderMixin
• REDUX batchActions
• REDUX Initializers
REACT & REDUX