无法在反应中显示来自减速器的消息

士兵

我试图message 'hello brozz..'从减速器显示到组件render()方法。ComponentDidMount()得到this.props.message作为请看图片我的减速机就像

 export const geod = (state ={}, action) => {
      console.log('inside reducer');
        switch(action.type){


            case 'INITIAL_MESSAGE':

              return [
                        ...state, {
                          message:'hello brozz..'
                        }
                      ]
            case 'CLICK_MESSAGE':
                  return [ 
                            ...state, { 
                                            message: ''
                                      }
                          ]
            default:
                    return state;
        }

     };

但是我不能在渲染方法中使用this.props.messageas <div><div>{this.props.message}</div></div>我收到错误

Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons

我的组件是,

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { initialMessageAction, clickButtonAction } from './redux.js';
import { connect } from 'react-redux';
import { Message } from './messageComponent';

class App extends Component {

  componentWillMount() {
    console.log('component will mount');
    let data = this.props.initialMessageAction();
  }
  componentDidUpdate() {
    console.log('comp Did Update')
    console.log(this.props.message)
  }
  render() {

    return (
      <div className="App">
        <div className="App-header">        
        </div>
        <p className="App-intro">
        react-redux-data-flow
        </p> 
              <div>{this.props.message}</div>
             <div>{this.props? <div>hi ..<button onClick={ () => this.props.clickButtonAction() }>hide message</button></div>: ''}</div>
      </div>
    );
  }
}


    const mapStateToProps = (state, ownProperty) => ({ 

    message:state.geod,   


  });   

   const mapDispatchToProps =  { 

     initialMessageAction,
    clickButtonAction

  } 

  export default connect(
                             mapStateToProps,
                             mapDispatchToProps

                    )(App);
安德鲁
const initialState = {message: null};
 export const geod = (state = initialState, action) => {
      console.log('inside reducer');
        switch(action.type){
            case 'INITIAL_MESSAGE':
                return Object.assign({}, state, {message:'hello brozz..'});
            case 'CLICK_MESSAGE':
                return Object.assign({}, state, {message: ''}); 
            default:
                return state;
        }

     };

而在stateMap { message: state.geod.message }

作为初始状态您使用一个空对象并且您看到它是错误的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

减速器中的打字稿类型问题

来自分类Dev

MR减速器中的输出HBase增量

来自分类Dev

反应redux减速器不改变

来自分类Dev

Redux:区分减速器中的对象

来自分类Dev

了解以下减速器中的扩散和破坏用途

来自分类Dev

在redux中是否可以嵌套化径减速器?

来自分类Dev

反应在父级减速器与子级减速器中完成的Redux逻辑

来自分类Dev

无法从承诺到减速器获得价值

来自分类Dev

减速器中CRUD的“ U”(更新)部分的问题

来自分类Dev

NgRx减速器中的拼接索引无法删除一个索引

来自分类Dev

在减速器中删除动态属性

来自分类Dev

减速器异常

来自分类Dev

减速器未在hadoop中执行

来自分类Dev

清理未在减速器中运行

来自分类Dev

React Redux无法让减速器采取行动

来自分类Dev

反应redux减速器不改变

来自分类Dev

无法将数据从动作传递到减速器

来自分类Dev

如何在不使用路由的情况下从反应中访问特定的减速器变量作为道具

来自分类Dev

Redux 存储无法从减速器获取状态

来自分类Dev

我的减速器已执行,但状态未在反应中更新

来自分类Dev

Redux 反应根减速器变慢

来自分类Dev

React Native 上的 Redux 无法从减速器渲染

来自分类Dev

在 react-native 中从另一个减速器更新减速器

来自分类Dev

反应:我有一些来自减速器的编辑状态错误

来自分类Dev

在反应中从另一个减速器修改状态

来自分类Dev

找不到模块:多个减速器时无法解析“./减速器”

来自分类Dev

减速器不更新反应 redux 中的状态

来自分类Dev

无法在组合减速器中使用减速器

来自分类Dev

redux 减速器中的打字稿