简体   繁体   English

对象在将道具从一个组件传递到另一个组件时,不能作为反应子对象(带有键的找到的对象)有效

[英]objects are not valid as a react child (found object with keys) while passing props from one component to other

I am passing a particular selected URL(data) from a component (List.js) to a component (DD.js), DD.js has two components, Left and Right, I want to display data in the Left component so have passed the props to left component, but I get the error saying 我正在将特定的选定URL(数据)从组件(List.js)传递到组件(DD.js),DD.js有两个组件,Left和Right,我想在Left组件中显示数据,因此左组件的道具,但我收到错误消息

Objects are not valid as a React child (found: object with keys {url}) 对象作为React子对象无效(找到:带有键{url}的对象)

. If you meant to render a collection of children, use an array instead. 如果要渲染子级集合,请改用数组。

DD.js DD.js

export default class DD extends Component {
  render(){
    let data = this.props.location.state
    return(
      <div className="dd-wrapper">
        <div className="left-drawer-wrapper">
          <Left url={data}/>
        </div>
        {/*right side section*/}
      </div>
    );
  }
}

Left.js Left.js

export default class Left extends Component{
  render(){
    let url = this.props.url
    return(
      <div>
        {url}
      </div>
    );
  }
}

List.js List.js

<tbody>
 {
   this.state.urls.map( (list,i) => {
   return(
     <tr key={i}>
       <td>
        <Link to={{pathname:`/service/${this.service_name}/requests`,
                        state:{url:this.state.urls[i]}}}
                        onClick={()=>this.selectedUrlHandler(i)}>{list} 
        </Link>
       </td>
       <td>{this.service_name}</td>
      </tr>
     );
   })
  }
</tbody>

I get the requested answer when i do console.log(url) , but when i remove it and do just {url} , the error occures , not sure what is going wrong . 当我执行console.log(url)时,我得到了要求的答案,但是当我删除它并仅执行{url}时,就会发生错误,不确定是怎么回事。

Since the url is passed an an object to the Left component which tried to render it you get an error. 由于url传递了一个对象到Left组件,该对象试图呈现该URL,因此会出现错误。 Instead just render the particular property of the state object like 而是只渲染state对象的特定属性,例如

export default class DD extends Component {
  render(){
    let data = this.props.location.state
    return(
      <div className="dd-wrapper">
        <div className="left-drawer-wrapper">
          <Left url={data}/>
        </div>
        {/*right side section*/}
      </div>
    );
  }
}

export default class Left extends Component{
  render(){
    let url = this.props.url.value
    return(
      <div>
        {url}
      </div>
    );
  }
}

You can not render an object as a children of a node in react component. 您不能将对象渲染为react组件中节点的子级。 To do so, you can stringify the object ie 为此,您可以对对象进行字符串化,即

export default class Left extends Component{
 render(){
  let url = this.props.url
  return(
    <div>
      {JSON.stringify(url)}
    </div>
  );
 }
}

OR 要么

You can render a particular value at a time from the object. 您可以一次从对象渲染特定的值。

export default class Left extends Component{
 render(){
  let url = this.props.url
  return(
    <div>
      {url.url1}
      {url.url2}
    </div>
  );
 }
}

The url is not a string it is an Object and react can not render Objects in JSX. 网址不是字符串,它是一个对象,因此无法在JSX中呈现对象。 You can check that by converting that to string. 您可以通过将其转换为字符串来进行检查。

<Left url={JSON.stringify(data)} />

暂无
暂无

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

相关问题 对象作为React子对象无效(找到:带有键{…}的对象) - Objects are not valid as a React child (found: object with keys {…}) 对象作为 React 子对象无效(找到:带有键的对象...) - Objects are not valid as a React child (found: object with keys…) 对象无效作为React子对象(找到:带有键的对象) - Objects are not valid as a React child (found: object with keys) 对象作为 React 子项无效(找到:object,键为 {}) - Objects are not valid as a React child (found: object with keys {}) React native - 对象无效作为React子对象(找到:具有键{$$ typeof,type,key,ref,props,_owner,_store}的对象) - React native - objects are not valid as a React child (found: object with keys {$$typeof, type, key, ref, props, _owner, _store}) React,错误:对象作为 React 子项无效(找到:object 和键 {data}) - React , Error: Objects are not valid as a React child (found: object with keys {data}) 对象作为 React 子对象无效(找到:object 和键 {})。 在本地机器(chrome浏览器)中工作,但不在生产和其他浏览器中工作 - Objects are not valid as a React child (found: object with keys {}). Working in local machine (chrome browser) but not production and other browser 错误:对象作为React子对象无效(找到:带有键的对象…) - Error: Objects are not valid as a React child (found: object with keys…) 如何修复“对象作为 React 子对象无效(找到:带有键 {} 的对象)” - How to fix "Objects are not valid as a React child (found: object with keys {}) 对象作为 React 子对象无效(找到:带有键 {author} 的对象) - Objects are not valid as a React child (found: object with keys {author})
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM