redux例子工程代码阅读

举报
Nick Qiu 发表于 2021/04/01 01:13:37 2021/04/01
【摘要】 简介 使用命令npx create-react-app my-app --template redux会创建一个redux的例子工程,本文讲解该工程的代码。 文件目录 counterSlice.js,redux 声明 初始化值 reducers actions Selectors Counter.js redux的使用 store.js 、index.js store的...

简介

使用命令npx create-react-app my-app --template redux会创建一个redux的例子工程,本文讲解该工程的代码。

  • 文件目录
  • counterSlice.js,redux 声明
    • 初始化值
    • reducers
    • actions
    • Selectors
  • Counter.js redux的使用
  • store.js 、index.js store的配置和引入

详细说明

文件目录

如下本文仅分析工程目录下的代码目录。

D:\TEST\MY-APP\SRC
│  App.css -  APP样式
│  App.js - 应用入口文件
│  App.test.js
│  index.css -  全局样式
│  index.js - js入口文件,调用App.js,启动react
│  serviceWorker.js
│  setupTests.js
│
├─app
│ store.js - store的全局入口
│
└─features └─counter Counter.js Counter.module.css counterSlice.js

counterSlice.js,redux声明

import { createSlice } from '@reduxjs/toolkit';

export const counterSlice = createSlice({
  name: 'counter',
  //1.初始化值
  initialState: { value: 0,
  },
  //2.声明多个reducers
  reducers: { increment: state => { // Redux Toolkit allows us to write "mutating" logic in reducers. It // doesn't actually mutate the state because it uses the Immer library, // which detects changes to a "draft state" and produces a brand new // immutable state based off those changes state.value += 1; }, decrement: state => { state.value -= 1; }, incrementByAmount: (state, action) => { state.value += action.payload; },
  },
});

// 3.导出reducers给外部action调用
export const { increment, decrement, incrementByAmount } = counterSlice.actions;

// The function below is called a thunk and allows us to perform async logic. It
// can be dispatched like a regular action: `dispatch(incrementAsync(10))`. This
// will call the thunk with the `dispatch` function as the first argument. Async
// code can then be executed and other actions can be dispatched
export const incrementAsync = amount => dispatch => {
  setTimeout(() => { dispatch(incrementByAmount(amount));
  }, 1000);
};

// The function below is called a selector and allows us to select a value from
// the state. Selectors can also be defined inline where they're used instead of
// in the slice file. For example: `useSelector((state) => state.counter.value)`
export const selectCount = state => state.counter.value;

export default counterSlice.reducer;

Counter.js redux的使用

import React, { useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import {
  decrement,
  increment,
  incrementByAmount,
  incrementAsync,
  selectCount,
} from './counterSlice';
import styles from './Counter.module.css';

export function Counter() {
  const count = useSelector(selectCount);
  const dispatch = useDispatch();
  const [incrementAmount, setIncrementAmount] = useState('2'); return ( <div> <div className={styles.row}> <button className={styles.button} aria-label="Increment value" onClick={() => dispatch(increment())} > + </button> <span className={styles.value}>{count}</span> <button className={styles.button} aria-label="Decrement value" onClick={() => dispatch(decrement())} > - </button> </div> <div className={styles.row}> <input className={styles.textbox} aria-label="Set increment amount" value={incrementAmount} onChange={e => setIncrementAmount(e.target.value)} /> <button className={styles.button} onClick={() => dispatch(incrementByAmount(Number(incrementAmount) || 0)) } > Add Amount </button> <button className={styles.asyncButton} onClick={() => dispatch(incrementAsync(Number(incrementAmount) || 0))} > Add Async </button> </div> </div>
  );
}

store.js 、index.js store的配置和引入

  • store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';

export default configureStore({
  reducer: { counter: counterReducer,
  },
});
  • index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import store from './app/store';
import { Provider } from 'react-redux';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
  <React.StrictMode> <Provider store={store}> <App /> </Provider>
  </React.StrictMode>,
  document.getElementById('root')
);
serviceWorker.unregister();

文章来源: www.jianshu.com,作者:Nick_4438,版权归原作者所有,如需转载,请联系作者。

原文链接:www.jianshu.com/p/d756daa262b2

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。