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
flux
andevents
-
Every state change will be done using dispatching actions. So we need something to dispatch these actions - so let’s create
AppDispatcher
insrc/AppDispatcher.js
See the docs. -
Let’s start from fluxifing toggling basic info from enroll view (
src/components/enroll/BasicInfo
). We want to changetoggleForm
to not usesetState
but 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
. Useregister
Dispatcher method to register for actions and handleTOGGLE_BASIC_INFO
actionType. -
From events docs, use
on
,emit
andremoveListener
for:
- emitting change after handled
TOGGLE_BASIC_INFO
action inEnrollStore
- subscribing to store change in component’s
componentWillMount
method - removing this subscription in
componentWillUnmount
method
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_INFO
string 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.js
file, so let’s add--require babel-core/register
to thedev
script inpackage.json
-
Open
server.js
and 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-router
library and try to use it inserver.js
IMPORTANT TIPS not mentioned there:
- you need to change
routes.js
to include onlyRoute
elements, you need to moveRouter
element to the upper level (application.js
). - you need to move creating history to the
application.js