简体   繁体   English

React对象作为React子错误无效

[英]React Objects are not valid as a React child error

I am trying to figure out what could be causing the error "Objects are not valid as a React child" in my code. 我试图找出导致我的代码中出现“对象作为React子无效的错误”的原因。 This is what I have so far: 这是我到目前为止的内容:

render() {
let showAddons = [];
            let addons = 0;
            if (typeof this.props.photoPackage.addons !== 'undefined'){
                if (typeof this.props.photoPackage.addons.Night !== 'undefined'
                    && this.props.photoPackage.addons.Night.isNight){
                    const price = this.props.photoPackage.addons.Night.price;
                    addons = addons + price;

                    showAddons.push(<li key={1}>Night Photos ${price}</li>)
                }
                if (typeof this.props.photoPackage.addons.Drone !== 'undefined'
                    && this.props.photoPackage.addons.Drone.isDrone){
                    const price = this.props.photoPackage.addons.Drone.price;
                    addons = addons + price;
                    showAddons.push(<li key={2}>Drone Photos ${price}</li>);

                }

return (
<div>
    {showAddons} //This causes the error
</div>
)}

The showAddons variable is what is causing the issue, but I can't figure out why. showAddons变量是引起此问题的原因,但我不知道为什么。 The price const in both if statements resolves to 99, I checked them by logging them to the console. 两个if语句中的price const都解析为99,我通过将它们记录到控制台进行了检查。 I did something very similar on another component and it works just fine: 我在另一个组件上做了非常相似的事情,并且效果很好:

render() {
let currentMonth = moment(this.state.year + '-' + this.state.month + '-' + 0o1);
            let times = [];
            let date = moment(this.state.chosenDate);
            console.log('Date ', date.format('DD'));
            for (let i = 0; i < Object.keys(this.state.times).length; i++){
                const day = moment(
                    date.format('YYYY-MM-DD')
                    + ' ' + this.state.times[i],
                    'YYYY-MM-DD hh:mmA');

               times.push(<li key={i} onClick={(e) => {
                    this.handleAddDateTime(day.format('YYYY-MM-DD hh:mm A'))


             }} className={"acuity_times"}>{day.format('h:mm A')} {(day.format ('h:mm A') === '6:00 PM' || day.format ('h:mm A') === '8:00 PM') ? '(+$99)' : ''}</li>)
            }

return (

   <div>
   {times} //This works without errors
   </div>


)}

Any help figuring this out would be appreciated. 任何帮助弄清楚这一点将不胜感激。

Update: 更新:

I have console.log() both the working and not working arrays and I took a photo of the console displaying the first object in both instances. 我有console.log()都在工作和不在工作的数组,我拍了控制台的照片,显示了两个实例中的第一个对象。 I don't know if it will help, but it might give some more insight: 我不知道这是否有帮助,但可能会提供更多见解:

在此处输入图片说明

This is the first object returned from the times array, which is the one that works as expected with no errors. 这是从times数组返回的第一个对象,该对象可以正常工作且没有错误。

在此处输入图片说明

This is the first object returned in the showAddons array, the one that produces the error. 这是在showAddons数组中返回的第一个对象,该对象会产生错误。 As you can see, they are both objects, but only showAddons is having the issue, whereas the times array works as expected. 如您所见,它们都是对象,但是只有showAddons存在问题,而times数组按预期工作。

Should it be () around return result instead of {} for normal React syntax? 对于正常的React语法,应该在返回结果附近加上()而不是{}吗?

I cannot get your second render function running without the same error. 没有相同的错误,我无法让您的第二个渲染功能运行。 I'm assuming your second function is a getItems render function so it can return an array. 我假设您的第二个函数是getItems渲染函数,因此它可以返回一个数组。

If so, you should wrap your {showAddons} inside a html tag or a React component. 如果是这样,您应该将{showAddons}包装在html标签或React组件中。

return (
  <div>
    {showAddons} //This causes the error
  </div>
)

You can replace div tag with React.Fragment if you're using react 16.2 or later. 如果您使用的是react 16.2或更高版本,则可以用React.Fragment替换div标签。

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

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