【问题标题】:Why my Component is not receiving updated props from Redux Store?为什么我的组件没有从 Redux Store 接收更新的 props?
【发布时间】:2018-12-24 21:13:01
【问题描述】:

我的组件没有从 redux 商店接收道具。我通过 onClick 事件调用 Action,这就是。单击时,Redux Store 中的名称“wrapper”更改为“wrapper slide-menu”,但是,当我执行 console.log() 时,Props 没有更新。 ,但是当我检查 Redux 开发工具时,显示商店已更新。

<a onClick={ ()=> this.props.setName("wrapper")} ><i className="zmdi zmdi-menu ti-align-right"></i></a>

这是我的组件的一部分,它是从 redux 商店更新的控制台日志侧边栏名称。

import React, { Component } from "react";
import "../App.css";
import { Link } from "react-router-dom";
class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sidebarname: this.props.sidebarname
    };
  }

  render() {
    const sidebarName = this.props.sidebarname;
    console.log("sidebarname is " + sidebarName);
    console.log(this.props);

这是我的行动。

import { connect } from "react-redux";
import * as Actions from "./indexActionTypes";
import App from "../../_layouts";

const mapStateToProps = state => ({
  sidebarname: state.sidebarReducer.sidebarname
});

const mapDispatchToProps = dispatch => ({
  setName: sidebarname => {
    //console.log('setName is called');
    //console.log('togglemargin is'.togglemargin);
    if (sidebarname !== "wrapper slide-menu") {
      dispatch({
        type: Actions.TOGGLE_SIDEBAR,
        sidebarname: sidebarname
      });
    }

    if (sidebarname === "wrapper") {
      console.log("this is called if2");
      dispatch({
        type: Actions.TOGGLE_SIDEBAR,
        sidebarname: "wrapper slide-menu"
      });
    }
  }
});

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(App);

这是我的 Reducer。

import * as Actions from "../../actions/indexToggle/indexActionTypes";

//const sidebarname = "wrapper slide-menu";
let initialState = { sidebarname: "wrapper" };

const sidebarReducer = (state = initialState, action) => {
  switch (action.type) {
    case Actions.TOGGLE_SIDEBAR:
      console.log("reducer called");
      console.log(state);
      //console.log('action',action);
      return Object.assign({}, state, {
        sidebarname: action.sidebarname
      });
    default:
      return state;
  }
};
export default sidebarReducer;

这是我的商店。

import { createStore, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import rootReducer from "../reducers/rootreducer";

//const  initialState = {};

const middleware = [thunk];
const store = createStore(
  rootReducer,
  compose(
    applyMiddleware(...middleware),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  )
);
export default store;

这是我的 App.js 文件。

import React, { Component } from 'react';
import './App.css';
import { Provider } from 'react-redux';
import store from './store/store';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import App from './actions/indexToggle/indexActions';
import FirstDashboard from './_layouts/views/firstDashboard';
import SecondDashboard from './_layouts/views/secondDashboard';
import ThirdDashboard from './actions/dashboardToggle/thirdDashboardToggleAction';
import FourthDashboard from './_layouts/views/fourthDashboard';
class Main extends Component {
  render() {
    return (
        <Provider store={store}>
          <Router>
              <div>
                     <App />
                     <Route path='/overview1' exact strict component={FirstDashboard} />
                     <Route path='/overview2' exact strict component={SecondDashboard} />
                     <Route path='/overview3' exact strict component={ThirdDashboard} />
                     <Route path='/overview4' exact strict component={FourthDashboard} />
              </div>
          </Router>
        </Provider>
    );
  }
}

export default Main;

我真的很困惑为什么它没有在组件内呈现。 但是,它正在商店中更新。

【问题讨论】:

  • const sidebarName = this.props.sidebarname; 应该是const sidebarName = this.state.sidebarname;
  • 请在你的 store 中注册你的 reducer const reducer = combineReducers({ staticReducer1, staticReducer2 }) const store = createStore(reducer, applyMiddleware(middleware)) @codemt
  • @atmd 我做到了。它尚未进行任何更改。
  • @VaibhavBhavsar 它已注册。我叫它。从'redux'导入{ combineReducers };从'../sidebarreducer/sidebarReducer'导入sidebarReducer;从'../marginreducer/marginreducer'导入marginReducer;导出默认 combineReducers({ sidebarReducer, });
  • 确保将默认的导出连接组件添加到反应渲染树中,而不是普通的 App 组件。

标签: javascript reactjs redux


【解决方案1】:

@codemt 请在您的代码中添加以下方法,它将接收更新的道具

  componentWillReceiveProps(nextProps) {

    const {sidebarname} = nextProps;

    this.setState({sidebarname});
  }

请在您的情况下放入您的相关组件是索引

【讨论】:

  • 你不必在 redux 中这样做。如果连接正确,Redux Provider 通过 store 将 props 注入组件。
猜你喜欢
  • 2021-01-21
  • 2020-08-10
  • 2017-10-20
  • 2017-01-25
  • 2020-09-22
  • 2022-01-16
  • 2018-09-27
  • 2019-01-17
  • 1970-01-01
相关资源
最近更新 更多