- Notifications
You must be signed in to change notification settings - Fork0
sandylib/react-hooker-boilerplate
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This project was bootstrapped withCreate React App.
You could use this quickly setup for react hook working with context api and use multiply reducer applications. No more redux needed anymore.
In the project directory, you can run:
Make sure you have Nodejs installed on your machine (I’m using version v12.6.0) and we’ll put the app together using,create-react-app:$ npx create-react-app react-hooker-boilerplate
Once it’s done, start it by running$ npm start in your project directory.Then open the package.json file. require node version 16.8 above
The "react-hooker-boilerplate" will follow Redux three fundamentalprinciples,stores,actions, andreducers. But build by useing context api working with useReducer
- let's start from create a store first, I have create configureStore.js under store folder
importReact,{createContext,useContext,useReducer}from'react';importrootReducer,{initialState}from'../reducers/index';constStateContext=createContext();constDispatchContext=createContext();exportconstuseStateValue=()=>useContext(StateContext);exportconstuseDispatch=()=>useContext(DispatchContext);functionStore(props){const[state,dispatch]=useReducer(rootReducer,initialState);return(<DispatchContext.Providervalue={dispatch}><StateContext.Providervalue={state}>{props.children}</StateContext.Provider></DispatchContext.Provider>);}exportdefaultStore;
since application grow will handle more and more complex state, so I create two separate reducer as example how to handle them
userReducer
import{UPDATE_SELECTED_USER}from'../constants/actionConstants';constuserReducer=(state,action)=>{switch(action.type){caseUPDATE_SELECTED_USER:return{ ...state,selected:action.selected}default:returnstate;}}exportdefaultuserReducer;
- bookReducer
import{UPDATE_SELECTED_BOOK}from'../constants/actionConstants';constbookReducer=(state,action)=>{switch(action.type){caseUPDATE_SELECTED_BOOK:return{ ...state,selected:action.selected}default:returnstate;}}exportdefaultbookReducer;
- rootReducer
importbookReducerfrom'./bookReducer';importuserReducerfrom'./userReducer';constrootReducer=({book, user},action)=>{// middleware goes here, i.e calling analytics service, etc.return{book:bookReducer(book,action),user:userReducer(user,action)}}exportconstinitialState={user:{list:[{"balance":"$3,946.45","picture":"http://placehold.it/32x32","age":23,"name":"Bird Ramsey","gender":"male","company":"NIMON","email":"birdramsey@nimon.com"}, ....],selected:null},book:{selected:null,list:[{"author":"Chinua Achebe","country":"Nigeria","imageLink":"images/things-fall-apart.jpg","language":"English","link":"https://en.wikipedia.org/wiki/Things_Fall_Apart\n","pages":209,"title":"Things Fall Apart","year":1958}, ...]}}exportdefaultrootReducer;
- action that work exactly like redux
import{UPDATE_SELECTED_USER}from'../constants/actionConstants';exportconstupdateSelectedUser=(dispatch,user)=>dispatch({type:UPDATE_SELECTED_USER,selected:user})
- UserComponent build in react hook and reader data from your stor and fire action
importReactfrom'react'import{useStateValue,useDispatch}from'../../store/configureStore';import{updateSelectedUser}from'../../reducers/user.action';constUserInfo=({user, selected, onClick})=>{consthandleClick=(user)=>e=>{onClick(user);};return(<tronClick={handleClick(user)}className={`${user===selected ?'hightlight' :''}`}><td>{user.balance}</td><td><imgsrc={user.picture}width={60}height={80}/></td><td>{user.age}</td><td>{user.name}</td><td>{user.gender}</td><td>{user.company}</td><td>{user.email}</td></tr>)}exportdefaultfunctionUserComponent(){const{user}=useStateValue();const{list, selected}=user;constdispatch=useDispatch();consthandleOnClick=user=>updateSelectedUser(dispatch,user);return(<table><tbody><tr><th>balance</th><th>picture</th><th>age</th><th>name</th><th>gender</th><th>company</th><th>email</th></tr>{list.map((item,idx)=><UserInfokey={idx}user={item}selected={selected}onClick={handleOnClick}/>)}</tbody></table>)}
- appRouter.js
importReactfrom'react';import{BrowserRouterasRouter,Route,Link}from"react-router-dom";import{BookComponent,UserComponent,Home}from'./components/';constAppRouter=()=>{return(<Router><div><ul><li><Linkto="/">Home</Link></li><li><Linkto="/user">User</Link></li><li><Linkto="/book">Book</Link></li></ul><hr/><Routeexactpath="/"component={Home}/><Routepath="/user"component={UserComponent}/><Routepath="/book"component={BookComponent}/></div></Router>)}exportdefaultAppRouter;
- App.js
importReactfrom'react';importAppRouterfrom'./AppRouter';import'./App.css';importStorefrom'./store/configureStore';functionApp(){return(<Store><AppRouter/></Store>);}exportdefaultApp;
- middleware that could be build a separeat file and put it into my rootReduce
constrootReducer=({book, user},action)=>{// middleware goes here, i.e calling analytics service, etc.return{book:bookReducer(book,action),user:userReducer(user,action)}}
As you can see with the power of the context api and react hooks it can entirely get rid of redux but working like redux application.
About
react-hooker-boilerplate : You could use this quickly setup for react hook working with context api and use multiply reducer applications. No more redux needed anymore.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
