rdx Flashcards
redux: to write a reducer, type
in reducers/classNameReducer.js
import { FETCH_CLASS_NAME, CREATE_CLASS_NAME } from ‘../actions/types.js’
const initialState = {
items: [],
items2: []
}
export default function (state = initialState, action) {
switch (action.type) {
case FETCH_CLASS_NAME:
return {…state, items: action.payload} // spread prior state, then overwrite the key
case CREATE_CLASS_NAME:
return {…state, items2: [{id:30, title: ‘string’}, …state.items]}
default:
return state
}
}
redux: To map parts of the state that come in from your reducer to this.props,
and to map action functions to this.props, type
import {connect} from ‘react-redux’
const mapStateToProps = (state, props) => ({
propName: state.nameInRootReducer.items,
propName2: state.nameInRootReducer.items2
})
const mapDispatchToProps = {
actionFunc1: thunkFunc1,
actionFunc2: actionFunc2
}
…
export default connect(mapStateToProps, mapDispatchToProps)(componenName);
remember:
not export default function
notes:
bindActionCreators in mapDispatchToProps is optional, and if you just use an object literal it happen automatically.
https://blog.isquaredsoftware.com/2016/10/idiomatic-redux-why-use-action-creators/
redux: To create a thunk action creator, type
in actions/classNameActions.js
import { FETCH_CLASS_NAME, CREATE_CLASS_NAME } from ‘./types’
note: This is a thunk, not an action creator
export const fetchClassName = () => (dispatch) => { fetch('http://url.com/') .then(response => response.json()) .then(data => dispatch({ type: FETCH_POSTS, payload: data // dispatch is like pub sub, i can send data to any reducer from here })) }
note: dispatch hes to be called in the thunk creator because that where the promise will resolve asynchronously.
promises are necessary when you are
relying on the return value of a function
To return a new object that combines multiple objects into one, type
Object.assign({}, {key: ‘string’}, {key2: ‘string2’})
or
{…{}, …{key: ‘string’}, …{key2: ‘string2’}}
Note: Repeated names will be overwritten by the later name.
The two ways to define a method in an object literal are
{ key: "string", funcName() { return "Luke" }, } Note: still requires commas or { key: "string", funcName: function() { return "Luke" } }
To be able to call a function using objectName.funcName = “string” and have the object you put after the = be used as the parameter, type
{ set funcName(param) { this.privateVarName.push(param); }, privateVarName: [] }
Note: Private vars are not accessible from instances because they don’t use this.varName
the get and set keywords do work in
object literals, as well as constructor functions and classes.
To create a getter function that can be called without using (), type
{ get funcName() { return "string" } }
To generate a class based component with all lifecycle methods in vscode, type
rcfc tab
To generate a functional component in vscode, type
rscp
Only if you do not set mapDispatchToProps, you can use
this.props.dispatch({type: ‘INCREMENT’})
redux automatically adds
as this.props.dispatch() in your component if you do not set mapDispatchToProps()
an action is
an object with type and payload
{type : “ADD_TODO”, payload : “string”}
converting an actionCreator into a closure is only necessary if
you need to make an async call before dispatching.
Note: You can dispatch before and after too.
thunk: converting an actionCreator into a closure is only necessary if
you need to make an async call before dispatching.
Note: You can dispatch before and after too.
best redux tutorial, aside from calling mapDispatchToProps mapActionsToProps, is
https://www.youtube.com/watch?v=OSSpVLpuVWA
and potentially for saying bindActionCreators is necessary
use this for the redux chrome dev tools
https://youtu.be/OSSpVLpuVWA?t=1355
rn: you should not use this.setState() in
componentDidMount
to make initial api request for data, use
componentDidMount
a thunk action can
dispatch an actionCreator from above it in the actions file.
need to learn the proptype shortcuts
rpt
pta ptb ptr ptn pto pts
https://marketplace.visualstudio.com/items?itemName=sledsworth.react-redux-es6-snippets
The proptype shortcuts are
rpt to create main, then
pta ptb ptf ptn pto pts
note: add r to end for required
https: //marketplace.visualstudio.com/items?itemName=sledsworth.react-redux-es6-snippets
React: To add a key to a list that is being mapped into JSX, type
const itemArray = [‘string1’, ‘string2’, ‘string3’];
const myJxsArray = itemArray.map((item, key) => -li key={key}>{item}-/li> );