为了账号安全,请及时绑定邮箱和手机立即绑定

这个地方的react报错是什么原因呢

这个地方的react报错是什么原因呢

江户川乱折腾 2019-03-08 18:15:56
百度了一会儿也没找到原因 --浏览器抛出的错误信息是Uncaught Error: 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. Check the render method of App.import React , { PureComponent,Component } from 'react'import localStore from '../util/localStore.js'import {cityName} from '../config/localStoreCity.js'import  { connect } from 'react-redux'import { bindActionCreators } from 'redux'import * as actions from '../actions/action'class App extends React.Component {  constructor(props,context){      super(props,context);      this.state={        inintdone:false      }  }  render() {    return(      <div>        <div  >{this.props.reducerCity}</div>        {          this.state.inintdone ?          this.props.children          :<span> 加载中1...</span>        }      </div>    )  }  componentDidMount(){    let name = localStore.getItem(cityName)    if (name ==null){        name='北京'    }    setTimeout(()=>{      this.setState({        inintdone :true      })    },1000)    this.props.action.localCity({            cityName: name,    })      }}function  mapStateToProps(state){  console.log(state);  return state}function mapDispatchToProps(dispatch) {    return {        action: bindActionCreators(actions, dispatch)    }}export default connect(    mapStateToProps,    mapDispatchToProps)(App)我的想法是dispatch city以后 <div >{this.props.reducerCity}</div> 这里的城市会对应的变化,但是为什么金额会报错呢。而且我在mapStateToProps这里console了一下。为什么会执行了两遍。第一遍参数是空。第二遍就有了我的对应的参数。。
查看完整描述

2 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

错误提示已经说了:
found: object with keys {},你的render函数里写的是

<div  >{this.props.reducerCity}</div>

实际上看了你的代码应该是

<div  >{this.props.reducerCity.cityName}</div>

执行两边可能是因为你settimeout修改了state,会导致重新渲染页面


查看完整回答
反对 回复 2019-03-24
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

问题出在this.props.reducerCity


查看完整回答
反对 回复 2019-03-24
  • 2 回答
  • 0 关注
  • 924 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号