You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add exact prop if necessary (anything that starts with / will also render in the above example)
<!-- Replace <a> tags with <Link> tags --><Linkto="/">Home</Link>
NavLink is Link but for styling
Page Redirects
// After 2 seconds, redirect to '/path'setTimeout(()=>{props.history.push('/path')},2000);
Router automatically adds its information to props for any component it loads up
<Route component={...} />
withRouter()
exportdefaultwithRouter(component)
Higher order component
Applies Router props to the component
Switch
Only match one route
Wraps <Route/> components, goes top down and looks to match the first link only and stops
Higher Order Components
Wraps a component and gives them extra features
constHoc=(WrappedComponent)=>{// if the wrapped component received any props, return themreturn(props)=>{return(// return jsx<WrappedComponent{...props}/>// spread props back into component)}}
Images
Import image: import Image from '/path'
<imgsrc={Image} />
Redux
Have central storage for data
Passing around props is bad
Define central store with redux
A component subscribes to changes in data; Redux passes data using props
To make a change:
Dispatch action
Actions describe changes with data payload
Action passed to Reducer
Reducer updates data store
const{ createStore }=Redux;// default stateconstinitState={todos: [],posts: []}functionmyreducer(state=initState,action){if(action.type=='ADD_TODO'){// return a new object representing the new statereturn{
...state,// all the other untouched propertiestodos: [...state.todos,action.todo]}}}conststore=createStore(myreducer);// listen to changes to store and react to themstore.subscribe(()=>{console.log('state updated');console.log(store.getState());})// type describes actionconsttodoAction={type: 'ADD_TODO',todo: 'play piano'}// send action to Reducerstore.dispatch(todoAction);
connect is a function that returns a HOC that wraps the component
connect takes in a parameter which maps the data from the store to the component props
// takes in store state as a propconstmapStateToProps=(state)=>{return{// store properties we want to return// ex.posts: state.posts}}
// another example from react-planimport{connect}from'react-redux'// takes state of store and returns object representing propsconstmapStateToProps=state=>{return{projects: state.project.projects}}exportdefaultconnect(mapStateToProps)(Dashboard)
connect listens for changes and calls mapStateToProps where we specify which props we provide to the component
In this case, we provide a new prop called projects
Map Dispatch to Props
To change state dispatch an action
// takes dispatch method as paramconstmapDispatchToProps=(dispatch)=>{return{// similar to mapStateToProps, maps properties to props of component// ex.deletePost: (id)=>{// we dispatch this action whenever deletePost is calleddispatch({type: 'DELETE_POST',id: id})}}}exportdefaultconnect(mapStateToProps,mapDispatchToProps)(Component)
importauthReducerfrom'./authReducer'importprojectReducerfrom'./projectReducer'import{combineReducers}from'redux'constrootReducer=combineReducers({// which reducers we want to combine and what we want to call themauth: authReducer,project: projectReducer,})exportdefaultrootReducer