A WIP project to learn React+Redux concepts, and setup a project scaffold.
Largely adopted from: https://www.valentinog.com/blog/redux/
npm install
npm start/src/js/actionsaction object definitions/componentsreact UI components/constantse.g. action type string defs/middlewarecustom middleware components, e.g. to validate an action payload before it's passed to a reducer/reducersredux reducer components/storeredux store definitionindex.jsinitialises react app wired to redux (Provider wraps up your React application and makes it aware of the entire Redux’s store)
index.htmlroot html entry pointindex.jsroot js entry point
React- Rendering of UI components
- Ideally I want to move to use the Presentation/Container pattern, but first - baby steps.
- Wiring of events and state to Redux components
- Routing (react-router)
- Rendering of UI components
Redux- All the state things
Store- The redux store is fundamental: the state of the whole application lives inside the store.
Reducers- The state in redux comes from reducers, reducers produce the state of your application
- A reducer is just a JavaScript function. A reducer takes two parameters: the current state and an action
- The reducer calculates the next state depending on the action type. Moreover, it should return at least the initial state when no action type matches.
ActionsandAction creators- In redux, an action is a simple object with a type, and a payload, e.g.
{ type: 'LIKE_POST', payload: { POST_ID: 123 } }
- An action creator is a function that may contain some logic, which is called by a reducer
/src/js/actions/**.js
- In redux, an action is a simple object with a type, and a payload, e.g.
Middleware- Redux middleware is code (a funciton) that intercepts actions coming into the store via the dispatch() method.
- Middlewares in Redux are super important because they will hold the bulk of your application’s logic.
The nice thing is that while inside the middleware you can access
getStateanddispatch
react-thunk- By default, we can’t use action creators for async tasks because they are expected to return an object (i.e. not a promise/callback function).
- redux-thunk middleware is used to solve this problem.
What is actually happening when I add a post?
- The
PostFormcomponent is connected to Redux- It maps the
addArticleaction to the ReduxdispatchAPI method and binds to the formsonSubmitevent.
- It maps the
- When you click the submit button:
onSubmitevent fired, triggershandleSubmithandleSubmituses a local (non Redux) state to grab the post title and use to trigger theaddArticleaction in itspropsthis.props.addArticleis mapped to Redux dispatch method so it triggers theaddArticleaction- the store has been configured with
forbiddenWordsMiddleware, so the action is processed here first.- if a forbidden word is detected, adding the article is blocked and a message shown
- else if valid article
- the action passed through to the reducer, which returns a new copy of the state which contains the new article paylod (no mutation!)
- Great tool to allow you to see exactly what's happnening under the hood
- Timetravel!
