简体   繁体   English

React和Redux:为什么在使用mapStateToProps时我的值没有从reducer中显示出来

[英]React and Redux: why isn't my value not displaying from reducer, when using mapStateToProps

In my react project, I've hooked up redux and added a reducer with an object that holds a key and value pair: "key: 'hello'". 在我的react项目中,我连接了redux并添加了一个reducer和一个对象,该对象包含一个键和值对:“ key:'hello'”。 I imported it into my index.js in my reducers folder, and am trying to use it inside a container, using MapStateToProps. 我将其导入我的reducers文件夹中的index.js中,并尝试使用MapStateToProps在容器内使用它。 For whatever reason, the value isn't showing up! 无论出于什么原因,价值都没有显示出来! When I console.log this.props.reducer, I am returned the correct object, but when I type in this.props.reducer.key I'm returned 'undefined'. 当我console.log this.props.reducer时,我返回了正确的对象,但是当我键入this.props.reducer.key时,我返回了“未定义”。 Why is this happening? 为什么会这样呢? I've never had this problem, before... 我从来没有遇到过这个问题,之前...

Here is my App.js 这是我的App.js

import React, { Component } from 'react';
import FirstComponent from './components/firstComponent.js';
import FirstContainer from './containers/firstContainer.js';

class App extends Component {
  render() {
    return (
      <div className="App">
        <FirstComponent />
        <FirstContainer />
      </div>
    );
  }
}

export default App;

my reducer: 我的减速器:

export default function() {
    return [
        {
            key: 'hello',
        }
    ]
}

my index.js inside the reducers folder 我在reducers文件夹中的index.js

import { combineReducers } from 'redux';
import firstReducer from './reducer.js';

const rootReducer = combineReducers({
    reducer: firstReducer
});

export default rootReducer;

my container: 我的容器:

import React from 'react';
import { connect } from 'react-redux';

class FirstContainer extends React.Component {
    render() {
        return(
            <div>
                value: {this.props.reducer.key}
            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        reducer: state.reducer
    };
}

export default connect(mapStateToProps)(FirstContainer);

I should see: 'value: hello' on my screen, but I just see 'value: ' displayed 我应该在屏幕上看到:“值:你好”,但我只看到显示了“值:”

why your reducer returns an array and doesn't accept any arguments? 为什么您的reducer返回一个数组并且不接受任何参数?

usually the reducer looks somewhat like this, so it accepts 2 params (first one being the previous state of the app) and returns an object 通常,Reducer看起来像这样,因此它接受2个参数(第一个参数是应用程序的先前状态)并返回一个对象

const initialState = { key: 'not hello' };

expert default functions(prevState = initialState, value) {
   const newStateObject = { key: value };

   return newStateObject;
}

Your reducer is incorrect, you can set the initial state, and thereafter the function is either supposed to return the state unchanged, or the next state if it receives an action it's interested in. 减速器不正确,您可以设置初始状态,此后该函数应该返回不变的状态,或者如果它收到感兴趣的操作则返回下一个状态。

const initialState = { key: 'hello'};

export default function(state = initialState, action) {
    switch (action.type) {
       // dispatch({ type: 'UpdateKey', value: 'new value' })
       case 'UpdateKey': {
         return {
           ...state,
           key: action.value,
         };
       }
    }

    return state;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM