Redux-mapStateToProps 不起作用

sai*_*025 4 javascript reactjs redux

我有一个反应组件,它在 componentWillMount 中进行 AJAX 调用,并支持接收到的数据以响应 redux 存储。这是代码

componentWillMount() {
  var self = this;
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (this.readyState === 4 && this.status === 200) {
      console.log(this.responseText);
      var json = JSON.parse(this.responseText);
      var data = {
        json
      };
      self.props.dispatch({
        type: "ADD_Exams",
        data
      });
    }
  };
  xmlhttp.open("GET", "http://127.0.0.1:8000/getExams/", true);
  xmlhttp.send();
}
Run Code Online (Sandbox Code Playgroud)

在减速器中,我将在操作中接收到的数据分配给减速器状态中定义的数组。

const initialState = {
  exams:[]    
}

const examreducer = (state = initialState, action) => {
  switch (action.type) {
    case "ADD_Exams":
      return {
        ...state,
        exams: [...state.exams, action.data.json]
      };
    default:
      return state;
  }
};
Run Code Online (Sandbox Code Playgroud)

但是当我使用 mapStateToProps 读取考试变量时,我得到了未定义。

const mapStateToProps = (state) => {
    return {
        exams: state.exams
    }
}

export default connect(mapStateToProps)(Exam); 
Run Code Online (Sandbox Code Playgroud)

我正在创建这样的商店

import { Provider } from "react-redux";
const store = createStore(loginReducer, examReducer);
ReactDOM.render(
  <Provider store={store}>
    <Exam />
  </Provider>,
  document.getElementById("root")
);

registerServiceWorker();
Run Code Online (Sandbox Code Playgroud)

console.log(this.props.exams) 打印未定义。这里有什么问题?

Lui*_*v99 11

我花了无数个小时寻找问题所在,为什么我的道具没有得到任何信息。

我关注了@Romain Pellerin 并且发现了一些东西

我最初的 mapStateToProps 是这样的:

const mapStateToProps = (state) => {


  return {
      userInfo: state.userInfo,
      loading: state.loading,
      error: state.error
  };
Run Code Online (Sandbox Code Playgroud)

}

按照 Romain 的回答,我控制台记录并观察了我的状态对象的输出,发现我必须指定具有我需要访问的状态的特定减速器!因为我使用了 combineReducers,所以也许您必须在 mapStateToProps 中指定要访问的 reducer。Soo 我不得不将我的代码更改为:

const mapStateToProps = (state) => {
    console.log(state);

    return {
        userInfo: state.THE_SPECIFIC_REDUCER.userInfo,
        loading: state.THE_SPECIFIC_REDUCER.loading,
        error: state.THE_SPECIFIC_REDUCER.error
    };
}
Run Code Online (Sandbox Code Playgroud)

这是我结合减速器的代码:

const rootReducer = combineReducers({
  loginReducer,
  THE_SPECIFIC_REDUCER
});
Run Code Online (Sandbox Code Playgroud)

Soo,鉴于我试图获得“THE_SPECIFIC_REDUCER”状态,我必须在 mapStateToProps 中指定它,如下所示:

myProp: state.THE_SPECIFIC_REDUCER.theThingIWantToAccess
Run Code Online (Sandbox Code Playgroud)

我希望这对某人有用!


小智 9

我遇到了同样的问题。我的错误是因为我对组件进行了错误的导出/导入。

出口:

export class MyComponent extends React.Component
...
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);
Run Code Online (Sandbox Code Playgroud)

进口:

import { MyComponent } from './MyComponent';
Run Code Online (Sandbox Code Playgroud)

为了解决我删除了类上的导出并使用了默认值:

import MyComponent from './MyComponent';
Run Code Online (Sandbox Code Playgroud)

  • 这结束了我 4 个小时的 Redux、thunk 和存储内容的尝试。 (3认同)

Rom*_*rin 4

你能像这样编辑你的内容mapStateToProps以查看你的状态的实际内容吗?

const mapStateToProps = (state) => {
   console.log(state);
    return {
        exams: state.exams
    }
}
Run Code Online (Sandbox Code Playgroud)

我怀疑你的减速器不是你的减速器的根源。如果是这样,您可能需要这样做state.examreducer.exams。另外,你的组件是否包裹在Provider?您需要它来访问上下文(通过它可以访问您的状态)。

更新

确保在渲染任何 React 组件之前初始化您的存储。

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';

const store = createStore(combineReducers({loginReducer, examReducer}), {loginReducer:{}, examReducer:{}});
ReactDOM.render(
  <Provider store={store}>
    <Exam />
  </Provider>,
  document.getElementById('root')
)
Run Code Online (Sandbox Code Playgroud)

然后更新您的mapStateToProps:

const mapStateToProps = (state) => {
    return {
        exams: state.examReducer.exams || []
    }
}
Run Code Online (Sandbox Code Playgroud)