当前位置:网站首页>Redux related usage
Redux related usage
2022-06-26 11:12:00 【DukeMr. Lee】
redux Is a separate single resource state Repository , It can be used in different frameworks .
Core code < Basic code >:
import { createStore } from "redux";
function couter(state = 0, aciton) {
switch (aciton.type) {
case "add":
return state + 1;
default:
return state;
}
}
const store = createStore(couter);
store.subscribe(function () {
console.log(" There are updates ", store.getState());
});
store.dispatch({ type: "add" });
Be careful : The page refresh warehouse will return to initialization
1. stay react Use steps in
(1) download redux Warehouse
Initialize the project with :npx create-react-app react-redux-example
The initialization project does not redux, Re added use :npm install redux react-redux
(2) entrance index.js in
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
// import reportWebVitals from './reportWebVitals';
import { Provider } from "react-redux";
import store from "./store";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
{/* 2. export Provider Incoming warehouse store */}
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
);
(3)App.js in
import "./App.css";
import { connect } from "react-redux";
function App(props) {
const handleClick = () => {
console.log(props, "===> Trigger click event acceptance props");
// 4. Modify the value in the status
const { dispatch } = props;
dispatch({
type: "add",
});
};
return (
<div className="App">
<h1>
{props.user}:{props.couter}
</h1>
<button onClick={handleClick}>Click me</button>
</div>
);
}
// 5. Create mapping , and connect(mapStateToProps)(App), stay props.couter Get the value
const mapStateToProps = (state) => {
console.log("mapStateToProps");
return {
user: state.user,
couter: state.couter,
};
};
// 3. use import {connect} from 'react-redux' Derived connect Connect App
export default connect(mapStateToProps)(App);
(4)src/store/index.js in
// 1. Create repository
import { createStore } from "redux";
import reducers from "./reducers";
const store = createStore(reducers);
store.subscribe(function () {
console.log(" There are updates ", store.getState());
});
export default store;
(5)src/store/reducers/index.js
import couter from "./couter";
import user from "./user";
// 6. Merge multiple states
import { combineReducers } from "redux";
export default combineReducers({
couter,
user,
});
(6)src/store/reducers/couter.js
function couter(state = 0, action) {
switch (action.type) {
case "add":
return state + 1;
default:
return state;
}
}
export default couter;
(7)src/store/reducers/user.js
function user(state = " Unknown ", action) {
switch (action.type) {
case "set":
return action.name;
default:
return state;
}
}
export default user;
Running results :
Deepen understanding redux Related connection :Redux Step by step , Section 1 :Redux Overview and concepts | Redux Chinese official website
边栏推荐
猜你喜欢
FasterRCNN
深度理解STM32的串口实验(寄存器)【保姆级教程】
9、 Beautify tables, forms, and hyperlinks
【深度学习理论】(7) 长短时记忆网络 LSTM
Nacos2.x.x start error creating bean with name 'grpcclusterserver';
2021 Q3-Q4 Kotlin Multiplatform 使用现状 | 调查报告
[work details] March 18, 2020
MySQL模糊查询详解
That is to say, "live broadcast" is launched! One stop live broadcast service with full link upgrade
2、 Linear table
随机推荐
2021 Q3-Q4 Kotlin Multiplatform 使用现状 | 调查报告
Jasperreports - print PDF (project tool)
Expand and collapse too high div
laravel-admin隐藏按钮, 及设置按钮显示, 默认序列, form 表单的不可修改值
Server single and two-way adjustable one key mutual trust script!
Work report (2)
Update mysql5.6 to 5.7 under Windows
关于印发《深圳市福田区支持战略性新兴产业和未来产业集群发展若干措施》的通知
高并发下如何防重?
9、 Beautify tables, forms, and hyperlinks
FasterRCNN
Redis的最佳实践?看完不心动,算我输!!
MySQL 30 military regulations
你好!正向代理!
Character sets and comparison rules
(Typora图床)阿里云oss搭建图床+Picgo上传图片详细教程
laravel-admin 用 原生JS实现声音提示,及自动播放
Bit operation n & (n-1), leetcode231, interview question 05.06
深度理解STM32的串口實驗(寄存器)【保姆級教程】
PC QQ大廳 上傳更新 修改versionInfo