鍍金池/ 問答/HTML5  HTML/ 這個(gè)地方的react報(bào)錯(cuò)是什么原因呢

這個(gè)地方的react報(bào)錯(cuò)是什么原因呢

百度了一會(huì)兒也沒找到原因 --瀏覽器拋出的錯(cuò)誤信息是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> 這里的城市會(huì)對(duì)應(yīng)的變化,但是為什么金額會(huì)報(bào)錯(cuò)呢。而且我在mapStateToProps這里console了一下。為什么會(huì)執(zhí)行了兩遍。第一遍參數(shù)是空。第二遍就有了我的對(duì)應(yīng)的參數(shù)。。
圖片描述

回答
編輯回答
別硬撐

錯(cuò)誤提示已經(jīng)說了:
found: object with keys {},你的render函數(shù)里寫的是

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

實(shí)際上看了你的代碼應(yīng)該是

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

執(zhí)行兩邊可能是因?yàn)槟鉺ettimeout修改了state,會(huì)導(dǎo)致重新渲染頁面

2017年9月7日 11:00
編輯回答
兔寶寶

問題出在this.props.reducerCity

2018年7月1日 06:14