react-redux使用

 1.创建store文件夹,分别创建子文件夹actions和reducers

2。创建constant.js

/* 
    该模块是用于定义action对象中type类型的常量值,目的只有一个:便于管理的同时防止程序员单词写错
*/
export const CHANGEVALUE = 'change_value'

3.store.js

import { legacy_createStore as createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import reducers from "./reducers";

export default createStore(reducers, applyMiddleware(thunk))

4.reducers创建index.js和redux_test.js

redux_test.js

import { CHANGEVALUE } from "../constant";

//定义一个默认状态
const defaultState = {
    msg: "你好,世界"
}

//导出一个函数
// eslint-disable-next-line import/no-anonymous-default-export
export default (state = defaultState, action) => {
    //从action对象中获取:type、data
    const { type, data } = action
    let newState = JSON.parse(JSON.stringify(state))//使用深拷贝,不然数据不变化
    switch (type) {
        case CHANGEVALUE:
            newState.msg = state.msg + data;
            break
        default:
            break
    }
    return newState
}

index.js

/*

    该文件用于汇总所有的reducer为一个总的reducer

*/

//引入combineReducers,用于汇总多个reducer

import { combineReducers } from 'redux'

import redux_test from './redux_test'



//汇总所有的reducer变为一个总的reducer

export default combineReducers({

    redux_test

})

5.reducers创建redux_test.js

import { CHANGEVALUE } from "../constant"

/* 
    该文件专门为Count组件生成action对象
*/
//同步action,就是指action的值为Object类型的一般对象
export const changeValue = data => ({ type: CHANGEVALUE, data })

测试=====

index.js使用Provider监听redux,传入store

import React from 'react';
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';//redux
import App from './App';
import store from './store/store';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  /* 此处需要用Provider包裹App,目的是让App所有的后代容器组件都能接收到store */
  <Provider store={store}>
    <App />
  </Provider>
);

 所有使用redux的组件放在containers目录下

创建ReduxTest组件,redux父组件与子组件使用connect连接

import React, { Fragment } from 'react'
import { connect } from 'react-redux'
import { changeValue } from '../../store/actions/redux_test'

function ReduxTest(props) {
    return (
        <Fragment>
            <div>
                {props.msg}
            </div>
            <button onClick={() => props.changeValue('----->hello')}>修改显示</button>
        </Fragment>
    )
}
//使用connect()()创建并暴露一个Count的容器组件
export default connect(
    state => ({
        msg: state.redux_test.msg
    }),
    {
        changeValue
    }
)(ReduxTest)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
最新版的 react-redux 使用方法如下: 首先,确保你的项目已经安装了 react-redux 依赖包。你可以使用以下命令安装它: ``` npm install react-redux ``` 或 ``` yarn add react-redux ``` 接下来,你需要创建一个 Redux store。你可以使用 Redux Toolkit 来简化这个过程。首先,导入 `configureStore` 和 `getDefaultMiddleware` 函数: ``` import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit'; ``` 然后,创建一个 rootReducer 来合并你的所有 reducer: ``` import { combineReducers } from 'redux'; const rootReducer = combineReducers({ // 这里是你的 reducer }); ``` 接着,使用 `configureStore` 函数创建 Redux store。你可以通过传入一个对象来配置 store,例如指定 reducer、middleware 等等。以下是一个示例: ``` const store = configureStore({ reducer: rootReducer, middleware: getDefaultMiddleware() }); ``` 现在,你可以使用 `<Provider>` 组件来将 Redux store 提供给你的整个应用程序。在你的根组件中,导入 `<Provider>` 组件和你的 Redux store,然后将其包裹在应用的最外层: ``` import { Provider } from 'react-redux'; ReactDOM.render( <Provider store={store}> <App /> </Provider>, document.getElementById('root') ); ``` 通过将 Redux store 提供给整个应用程序,你可以在应用的任何地方使用 `useSelector` 和 `useDispatch` 钩子来访问 Redux store 中的状态和分发 action。例如,在你的组件中,你可以这样使用: ``` import { useSelector, useDispatch } from 'react-redux'; const MyComponent = () => { const counter = useSelector(state => state.counter); const dispatch = useDispatch(); // 使用 counter 和 dispatch }; ``` 这就是最新版的 react-redux使用方法。你可以根据你的具体需求,自定义配置和使用其他相关的 react-redux API。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值