当前位置:网站首页>redux相关用法
redux相关用法
2022-06-26 10:11:00 【DukeMr.Lee】
redux是一个单独的单一资源状态库,可以用于不同的框架。
核心代码<基础代码>:
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("有更新", store.getState());
});
store.dispatch({ type: "add" });注意:页面刷新仓库会回到初始化
1.在react中使用步骤
(1)下载redux仓库
初始化项目时用:npx create-react-app react-redux-example
初始化项目没有redux,再添加的使用:npm install redux react-redux
(2)入口index.js中
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.导出Provider传入仓库store */}
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
);(3)App.js中
import "./App.css";
import { connect } from "react-redux";
function App(props) {
const handleClick = () => {
console.log(props, "===>触发点击事件接受props");
// 4.修改状态里的值
const { dispatch } = props;
dispatch({
type: "add",
});
};
return (
<div className="App">
<h1>
{props.user}:{props.couter}
</h1>
<button onClick={handleClick}>Click me</button>
</div>
);
}
// 5.创建映射,并connect(mapStateToProps)(App),在props.couter中获取值
const mapStateToProps = (state) => {
console.log("mapStateToProps");
return {
user: state.user,
couter: state.couter,
};
};
// 3.用import {connect} from 'react-redux'导出的connect连接App
export default connect(mapStateToProps)(App);
(4)src/store/index.js中
// 1.创建仓库
import { createStore } from "redux";
import reducers from "./reducers";
const store = createStore(reducers);
store.subscribe(function () {
console.log("有更新", store.getState());
});
export default store;(5)src/store/reducers/index.js
import couter from "./couter";
import user from "./user";
// 6.合并多个状态
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 = "未知", action) {
switch (action.type) {
case "set":
return action.name;
default:
return state;
}
}
export default user;运行结果:

深入了解redux相关连接:Redux 循序渐进,第一节:Redux 概述和概念 | Redux 中文官网
边栏推荐
- Grain Mall - High Availability Cluster
- sliding window
- Nuxt. JS - learning notes
- Vscode environment setup: synchronous configuration
- Update mysql5.6 to 5.7 under Windows
- MySQL 12th job - Application of stored procedure
- Server single and two-way adjustable one key mutual trust script!
- June training (the 26th day) - collective search
- Oracle11g 启动数据库时报错 ORA-27154: post/wait create failed
- Concise course of probability theory and statistics in engineering mathematics second edition review outline
猜你喜欢
随机推荐
2020.7.6 interview with fence network technology company
[echart] i. how to learn echart and its characteristic document reading notes
sysbench基础介绍
ISO 26262之——2功能安全概念
Moore vote, leetcode169, leetcode229
【北邮果园微处理器设计】10 Serial Communication 串口通信笔记
3、 Linked list exercise
互联网对抗神器之漏洞扫描与反渗透
Is it safe to open an account in the school of Finance and business?
Qixia housing and Urban Rural Development Bureau and fire rescue brigade carried out fire safety training
Easyexcel - Excel read / write tool
最牛X的CMDB系统
Reasons for "unresolved external symbols" during vs or QT compilation link:
代码规范 & 详细解释 husky、prettier、eslint、lint-staged 的作用和使用
CentOS安装Redis多主多从集群
Pit record_ TreeSet custom sorting results in less data loss
CentOS installs redis multi master multi slave cluster
即构「畅直播」上线!提供全链路升级的一站式直播服务
【深度学习理论】(6) 循环神经网络 RNN
June training (the 26th day) - collective search




![[work details] March 18, 2020](/img/24/a72230daac08e7ec5bd57df08071f8.jpg)



![Installing MySQL under Linux [details]](/img/38/77be56c3ef3923ce4c4e5df4a96f41.png)