当前位置:网站首页>Instructions for Redux
Instructions for Redux
2022-07-06 17:04:00 【Society, you Lei brother, life is hard, don't bend down】
Redux The space

The concept is introduced :
redux There are three core concepts
1 action
The object of action
contain 2 Attributes
type: Identity properties , The value is a string , only , Necessary attribute
data: Data attribute , Any value type , Optional attribute
2 reducer
- Used to initialize the State 、 Processing status .
- When processing , According to the old state and action, Generate new state The pure function of .
3 store
take state、action、reducer Connected objects ,
- getState(): obtain state
- dispatch(action): distribution action, Trigger reducer call , Generate new state
- subscribe(listener): Register to listen , When new state when , Automatically call
redux It can help us manage the state of data in a unified way , First installation redux
npm install --save redux
establish store.js file , introduce store Builder
import {
createStore,applyMiddleware} from 'redux';
introduce reducer
import countRecucer from './count_reducer';
/* introduce redux-thunk Asynchronous Support */
import thunk from 'redux-thunk';
// take store Expose
export default createStore(countRecucer,applyMiddleware(thunk));
establish count_reducer.js file
reducer The essence of is actually a function , There are two parameters , They are the previous states prestate And action objects action , Under initial conditions prestate by undefined, We can assign parameters
import {
INCREMENT,DEINCREMENT} from './constant';
let initCount = 0;
function countReducer(prestate=initCount,action){
const {
type,data} = action;
switch(type){
case INCREMENT:
return prestate+data;
case DEINCREMENT:
return prestate-data;
default:
return prestate;
}
}
export default countReducer;
establish constant.js file
Of this module action Object's type Property to set a constant value , The purpose is to standardize and prevent mistakes
export const INCREMENT = 'increment';
export const DEINCREMENT = 'deincrement';
establish count_action.js file
/* This component is specially designed for count Component generation action object */
import {
INCREMENT,DEINCREMENT} from './constant';
// import store from './store'
export const createIcrementAction = data =>({
type:INCREMENT,data});
export const createDeIcrementAction = data =>({
type:DEINCREMENT,data});
/* asynchronous action, Is to deal with in the function action, but store General objects are handled by default , So we need to introduce redux-thunk Middleware to support the processing of functions */
export const createAsyncIcrementAction = (data,time)=>{
/* The first parameter is store Added for us , Know that you need to handle functions , The default incoming , Avoid introducing additional store */
return (dispatch)=>{
setTimeout(() => {
dispatch(createIcrementAction(data));
}, time);
}
}
Used in components redux
import store from './redux/store';
import {
createIcrementAction,createDeIcrementAction,createAsyncIcrementAction} from './redux/count_actions'
increment = ()=>{
let value = this.sel.value;
store.dispatch(createIcrementAction(value*1));
}
deincrement = ()=>{
let value = this.sel.value;
store.dispatch(createDeIcrementAction(value*1));
}
incrementIfOdd=()=>{
if(store.getState() % 2 !==0){
let value = this.sel.value;
store.dispatch(createIcrementAction(value*1));
}
}
It should be noted that if the page responds to updates, you need to manually subscribe in the component
componentDidMount(){
store.subscribe(()=>{
this.setState({
});
})
}
react-redux In combination with
react-Redux Divide all components into two major categories ,UI Components and container components . It will help us automatically redux Data is linked to front-end data , You can realize two-way data binding without manual subscription .
UI Components
- Only responsible for UI Presentation , Without any business logic
- adopt props receive data ( General data and functions )
- Do not use any Redux Of API
Container components
- Responsible for managing data and business logic , Not responsible for UI Presentation
- Use Redux Of API
Use
index.js file
provider It can help us pass in at one time store, It will help us to store Pass on to UI Components to use
import store from './redux/store';
import {
Provider} from 'react-redux';
<Provider store={
store}>
<App/>
</Provider>
store.js file
const allReducer = combineReducers({
addcount:countRecucer,
addperson:personReducer
});
Used in components react-redux
/* introduce connect Used to connect to UI Components and redux, At the beginning, I received two functions as parameters , The first function can be called mapStateToProps Mapping , The first argument to the function is state data , The second function can be called mapDispatchToProps Mapping , The first parameter is dispatch Method I use the abbreviation below .*/
import {
connect} from 'react-redux'
export default connect((state)=>({
count:state.addcount,personnum:state.addperson}),
{
jia:createIcrementAction,
jian:createDeIcrementAction,
jiaAsync:createAsyncIcrementAction})(Count)
<p> The current sum is {this.props.count}</p>
<h4> The current number is :{this.props.personnum}</h4>
边栏推荐
- 唯有学C不负众望 TOP3 Demo练习
- 这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
- 这116名学生,用3天时间复刻了字节跳动内部真实技术项目
- 字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
- "One year after graduation, I won ACL best paper"
- Shell_ 03_ environment variable
- ~73 other text styles
- 我走过最迷的路,是字节跳动程序员的脑回路
- Ce n'est qu'en apprenant que c est à la hauteur des attentes Top5 s1e8 | s1e9: caractères et chaînes & opérateurs arithmétiques
- ~68 Icon Font introduction
猜你喜欢

~84 form supplement

程序员定位解决问题方法论
![Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]](/img/e6/c53ad67ead1793a2acb93c26e8d377.jpg)
Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]

ByteDance technical Interviewer: what kind of candidate do I want to pick most

Log4j2 major vulnerabilities and Solutions

Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)

IDEA断点调试技巧,多张动图包教包会。

算数运算指令

Install docker under windows10 (through Oracle VM VirtualBox)

字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
随机推荐
Shell_ 02_ Text three swordsman
ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021
MySQL digital function
Assembly language addressing mode
koa中间件
唯有学C不负众望 TOP4 S1E6:数据类型
Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
原型链继承
string. How to choose h and string and CString
DOS function call
Activit fragmented deadly pit
DS18B20數字溫度計系統設計
Only learning C can live up to expectations top5 S1E8 | S1E9: characters and strings & arithmetic operators
Activiti目录(一)重点介绍
Error occurred during initialization of VM Could not reserve enough space for object heap
MySQL optimization notes
Description of project structure configuration of idea
Koa Middleware
一个数10年工作经验的微服务架构老师的简历
Activiti目录(四)查询代办/已办、审核