react-redux 的使用

1 安装react-redux: npm install --save react-redux 

2.之前使用redux的store.subscribe监听 store的状态改变,通过store.getState函数获取store的状态值;并且需要划分ui组件和聪明组件,着实很麻烦,引入react-redux,可以改变这一切;

store定义如下,引入react-redux:

import { createStore, compose, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducer';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(reducer, composeEnhancers(
    applyMiddleware(thunk)
));

export default store;

3. 顶部组件在最外层引入store,这样所有的组件都可以使用store了;

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { Provider } from 'react-redux'
import store from './store'
const MyApp = (
    <Provider store={store}>
        <App/>
    </Provider>
)
ReactDOM.render(MyApp, document.getElementById('root'));

4.具体在组件中使用connect将 ui组件变成聪明组件:

import React, { Component } from 'react';
import './App.css';
import { connect } from 'react-redux'
import {changeInputValue,addInputValue,deleteListItem,getListData} from './store/actionCreator.js'
class App extends Component {
  constructor(props){
    super(props);
    this.state = {}
  }
  componentDidMount(){
    this.props.getListDatas();
  }
  render() {
    const {inputValue,changeInputValue,addListItem,list,deleteList} = this.props;
    return (
      <div className="App">
        <label htmlFor="box">
          输入信息
          <input id="box" 
            value = {inputValue} 
            onChange ={changeInputValue}
          />
          <button onClick={addListItem}>提交</button>
          </label>
          <ul>
          {
            list.map((item,index) => {
                return (
                  <li key={index} onClick={deleteList.bind(this,index) }>{item}</li>
                )
            })
          }
          </ul>
      </div>
    );
  }

}
const mapStateToProps = (state)=> {
  return (
    {
      inputValue:state.inputValue,
      list:state.list
    }
  )
};
const mapDispatchToProps = (dispatch)=>{
  return {
    changeInputValue(e){
      dispatch(changeInputValue(e.target.value))
    },
    addListItem(){
      dispatch(addInputValue())
    },
    deleteList(index){
      dispatch(deleteListItem(index))
    },
    getListDatas(){
      dispatch(getListData())
    }
  }
}
export default connect(mapStateToProps,mapDispatchToProps)(App);

 

转载于:https://www.cnblogs.com/xiaozhumaopao/p/10548370.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值