Modern React PP
Modern React PP
-We will work directly in the development environment, we will not use JS Fiddle or
Codepen.
-We will focus on the main fundamental concepts of React and Redux and avoid
styling.
Examples: Whether a user is logged in or not, the page a user is on, or whether a dialog box is open.
Examples of Non Temporary Data: blog posts, comments and user profile data.
For example, an action with the type “login_success” might change a property of Redux state called isAuthenticated
from false to true.
Action Creators
- Can dispatch actions asynchronously
- a function which dispatches a normal action
- Allows for dynamically setting the payload property
- No changes required to the reducer
For example if you want to save the user input to the state. You would setup the action creator with an arrow function like
so:
case ACTION_TYPES.LOGIN_SUCCESS:
return {
...state,
isAuthenticated: true,
}
mapStateToProps()
- Essentially, how to get the state from Redux
- Property in the return statement is user defined
- Set the value to “state.” (name of reducer if more than one) “ . ” (name of property from reducer)
- In the React render method use “this.props.” name of property you set.
mapDistachToProps()
- How to change the state by dispatching actions
- Actions can be plain or async action creators
- Actions can have an optional payload
- To dispatch actions use the syntax “this.props.” (name of the property you set)
connect ()
-Connects the React Container with the Redux store
Read and Actions Container Read Only Container Dispatch Actions Only Container
Link Comparing Redux Thunk with regular action creators, the difference in code is quite literally
2 words.
https://stackoverflow.com/questions/34570758/why-do-we-need-middleware-for-async-flow-i
n-redux
Hooks
React Hooks
Hooks are referring to being able to “hook” react class functionality to a functional component.
Main purpose of hooks is to give class functionality to react functional components, mainly to
allow functional components to read and update state.
Right:
useEffect( () => true ? return value : null )
4 Main Hooks Overview
useState() useEffect() useContext() useReducer()
useEffect will be called every useEffect will be called when useEffect will be called when
the component mounts state.value changes
time a component renders
useReducer()
import * as Reducer from './store/hooks/reducer’;
Import * as ACTIONS from ‘./store/actions/actions’;
**You can update the global state through either useReducer() or useState() in App.js
Migration Guide from Redux to React hooks
A lot of current projects still use Redux so it’s important to understand both approaches.
We will not change the existing React Redux code but build React Hooks separately from scratch