当前位置:网站首页>redux工作流程+小例子的完整代码
redux工作流程+小例子的完整代码
2022-06-25 22:13:00 【烦啦烦啦】
创建项目:
1.sudo npm install -g create-react-app
2.create-react-app redux
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {
Provider } from 'react-redux';
import store from './store';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<Provider store={
store}>
<App />
</Provider>
)
App.js
import React,{
Component} from 'react';
import * as Action from './store/action';
import {
connect} from 'react-redux';
class App extends Component {
constructor(props) {
super(props);
}
render() {
const {
value} = this.props;
return (
<div>
<input value={
this.props.value} style={
{
marginRight:'5px'}} />
<button onClick={
() => this.props.add(value)} style={
{
marginRight:'5px'}}>+</button>
<button onClick={
() => this.props.sub(this.props.value)}>-</button>
</div>
)
}
}
const mapStateToProps = (state) => ({
value:state.AppValue.value
})
const mapDispatchToProps = (dispatch) => ({
//加一
add(data) {
dispatch(Action.Add(data))
},
//减一
sub(data) {
dispatch(Action.Sub(data))
}
})
export default connect(mapStateToProps,mapDispatchToProps)(App);
action.js
import * as ActionTypes from "./actionType";
//加一
export const Add = (data) => {
var addData = data + 1;
return {
type:ActionTypes.ADDVALUE,
data:addData
}
}
//减一
export const Sub = (data) => {
var subData = data - 1;
return {
type:ActionTypes.SUBVALUE,
data:subData
}
}
actionType.js
export const ADDVALUE = 'add';
export const SUBVALUE = 'sub';
reducer.js
import * as ActionTypes from './actionType';
const reducer = (state = {
},action) => {
switch(action.type) {
case ActionTypes.ADDVALUE: {
return {
...state,value:action.data}
}
case ActionTypes.SUBVALUE:{
return {
...state,value:action.data}
}
default: {
return state;
}
}
}
export default reducer;
store.js
import {
combineReducers, createStore} from 'redux'
import AppReducer from './store/reducer'
const reducer = combineReducers({
AppValue:AppReducer
})
const initState = ({
AppValue: {
value:12
}
})
export default createStore(reducer,initState)
边栏推荐
- Building cloud computers with FRP
- MySQL master-slave replication
- 懒人教你用猕猴桃一月饱减16斤_过路老熊_新浪博客
- PCB生产为什么要做拼板和板边
- Oracle writes a trigger that inserts a piece of data first and updates a field in the data
- EasyConnect连接后显示未分配虚拟地址
- About Simple Data Visualization
- Thrift入门学习
- Sword finger offer 48 Longest substring without duplicate characters
- [wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)
猜你喜欢
![[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)](/img/d9/935bad29005e5846dc514c966e3b0e.png)
[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)

About the solution to prompt modulenotfounderror: no module named'pymongo 'when running the scratch project

手工制作 pl-2303hx 的USB轉TTL電平串口的電路_過路老熊_新浪博客

ASA如何配置端口映射及PAT

Mysql5.7.31自定义安装详细说明

懒人教你用猕猴桃一月饱减16斤_过路老熊_新浪博客

Hand made pl-2303hx USB to TTL level serial port circuit_ Old bear passing by_ Sina blog

详细讲解局部变量、全局变量、静态变量三种类型

Redis jump table

Literature research (I): hourly energy consumption prediction of office buildings based on integrated learning and energy consumption pattern classification
随机推荐
POSTMAN测试出现SSL无响应
Redis cluster
10.4.1、數據中臺
darkent2ncnn出错
MySQL master-slave replication
Common methods of object class
Number array de duplication in JS
redis之详解
解决线程并发安全问题
Explain in detail the three types of local variables, global variables and static variables
On the use of bisection and double pointer
Topic36——53. 最大子数组和
2021-04-28
猕猴桃酵素的功效_过路老熊_新浪博客
迅为RK3568开发板使用RKNN-Toolkit-lite2运行测试程序
SMT贴片加工PCBA板清洗注意事项
Object array de duplication
Redis memory elimination mechanism
Redis之常见问题
[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)