https://code-cartoons.com/a-cartoon-guide-to-flux-6157355ab207?gi=1f5594e56680#.ec8f3aghg
Flux: https://facebook.github.io/flux/docs/overview.html
Events: https://nodejs.org/api/events.html
-
Install locally package
fluxandevents -
Every state change will be done using dispatching actions. So we need something to dispatch these actions - so let’s create
AppDispatcherinsrc/AppDispatcher.jsSee the docs. -
Let’s start from fluxifing toggling basic info from enroll view (
src/components/enroll/BasicInfo). We want to changetoggleFormto not usesetStatebut to dispatch action. -
Modification of the global state should happen in the store (although you can have more than one store), so let’s create store in
src/stores/EnrollStore.js. UseregisterDispatcher method to register for actions and handleTOGGLE_BASIC_INFOactionType. -
From events docs, use
on,emitandremoveListenerfor:
- emitting change after handled
TOGGLE_BASIC_INFOaction inEnrollStore - subscribing to store change in component’s
componentWillMountmethod - removing this subscription in
componentWillUnmountmethod
The best for now would be to wrap event operations with methods in EnrollStore (we will move it out to more general place later).
- In the subscribed method in the component we can now finally update our local component state:
setState({ open: value })where value should be taken from the store (you need to add proper method)
Refactor code:
- create new folder -
action_creators- and new class there -EnrollActionCreator.js - move dispatching action to separate method in
EnrollActionCreator - create new folder -
constants- and new file there -ActionTypes.js - move
TOGGLE_BASIC_INFOstring there
Do the same for toggleForm in src/enroll/Preferences.js
Fluxify src/containers/Participants.js (there is setState in componentWillMount)
Open page source and see that we don’t have any content there : (
Notice that <div id=“app”></div> where our app should be rendered is empty.
It works perfectly client side, but it’s not server side, so our SEO sucks : (
Let’s take first step to make it make it better.
-
We need to enable ES6 in
server.jsfile, so let’s add--require babel-core/registerto thedevscript inpackage.json -
Open
server.jsand change it to use ES6 (imports, vars to consts, etc). References: Babel or E6-features.
- Take a look on server side rendering doc of
react-routerlibrary and try to use it inserver.js
IMPORTANT TIPS not mentioned there:
- you need to change
routes.jsto include onlyRouteelements, you need to moveRouterelement to the upper level (application.js). - you need to move creating history to the
application.js