react-redux 使用步骤

第一步

安装 redux 相关的包:yarn add redux react-redux redux-thunk redux-devtools-extension axios

第二步

在 store 目录中分别创建:actions 和 reducers 文件夹、index.js 文件
/store
  /actions
    login.js
    index.js
  /reducers
    login.js
    index.js
  index.js


//store  index.js
import { createStore, applyMiddleware } from 'redux'
import rootReducer from './reducers'   //状态

import thunk from 'redux-thunk' //中间件
import { composeWithDevTools } from 'redux-devtools-extension'
const middlewares = composeWithDevTools(applyMiddleware(thunk))

const store = createStore(rootReducer, middlewares)
export default store

//store/actions/index.js
//引入所有模块
export * from "./login";
export * from "./user";
export * from "./article";

//store/actions/login.js

import axios from "axios";
import { setToken ,clearToken} from "@/utils";
export const login = (mobile, code) => {
  return async (dispatch) => {
    const res = await axios.post(
      "http://geek.itheima.net/v1_0/authorizations",
      {
        mobile,
        code,
      }
    );
    const { token } = res.data.data;

    dispatch({ type: "login/token", payload: token });

    setToken(token);
  };
};

export const logout = () => {
  return (dispatch,getState)=>{
    clearToken()
    dispatch({type : 'login/logout'})
    dispatch({type : 'user/logout'})
  }
}


//store/reducers/index.js
import { combineReducers } from 'redux'

import login from './login'
//模块化 
const rootReducer = combineReducers({
  login
})

export default rootReducer

//store/reducers/login.js 
// 状态默认值为:''
const initialState =  '';
const login = (state = initialState, action) => {
  switch (action.type) {
    case "login/token":
      return action.payload;
      case "login/logout":
      return initialState;
    default:
      return state;
  }
};

export default login

//根文件/index.js  相当于main.js文件
//必须引入
import { Provider } from 'react-redux'
import store from './store'

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.querSelector('#root')
)

获取值

import store from '@/store'
  console.log(store.getState());

import { useSelector } from 'react-redux'
// 比如,Redux 中的状态是个对象,就可以:
const list = useSelector(state => state.list)

使用 Redux 的套路: 组件 dispatch 异步 action -> 提供异步 action -> 完成异步操作 -> 继续 dispatch 普通 action 来发起状态更新 -> reducers 处理状态更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React-Redux一个结合了ReactRedux的库,它使得在React应用集成状态管理变得更加容易。Redux一个单一来源的状态管理库,而React-Redux提供了一组工具来帮助你将Redux的状态管理与React组件结合起来。 安装React-Redux步骤通常是这样的: 1. 首先,确保你已经安装了Node.js和npm(Node包管理器)。 2. 在你的项目目录,打开终端或命令提示符。 3. 使用npm或yarn来安装`react-redux`和`redux`库。如果你还没有安装`redux`,可以先安装: ```bash npm install redux ``` 或者使用yarn: ```bash yarn add redux ``` 4. 安装`react-redux`: ```bash npm install react-redux ``` 或者: ```bash yarn add react-redux ``` 5. 引入到你的项目。通常会创建一个store来保存全局状态,以及将store连接到React组件上: ```javascript // 在index.js或App.js import { createStore } from 'redux'; import rootReducer from './reducers'; // 根 reducer,合并所有模块的reducer const store = createStore(rootReducer); // 如果你在使用react-redux,还需要引入Provider组件: import { Provider } from 'react-redux'; import App from './App'; // 你的React应用组件 function Root() { return ( <Provider store={store}> <App /> </Provider> ); } ReactDOM.render(<Root />, document.getElementById('root')); ``` 6. 在React组件使用`connect`函数从store提取数据并处理dispatch动作: ```javascript import { useSelector, useDispatch } from 'react-redux'; function MyComponent() { const myState = useSelector(state => state.mySlice); // 选择store的状态 const dispatch = useDispatch(); // 获取dispatch方法 // 在组件内部使用state和dispatch // ... } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值