Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React : Invariant Violation: Objects are not valid as a React child

I am working on ReactJS Search filter , Currently I am facing a problem when I enter match input application is crashed and give this error Objects are not valid as a React child (found: object with keys {id, companyName, account, venueCode, openDate, website, primaryPhone, emailAddress, description, firstName, lastName, active, title, department, officePhone, mobilePhone, tenantId, hidden, deleted, parentId}). If you meant to render a collection of children, use an array instead. Somebody please help me how to solve this problem . I am beginner and don't have much knowledge to resolve this problem . First time application is rendering successfully when I enter some match input it give me an error .

Code

        class Example extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      Item: 5,
      skip: 0
    }

    this.handleClick = this.handleClick.bind(this);
  }

  urlParams() {
    return `http://localhost:3001/meetups?filter[limit]=${(this.state.Item)}&&filter[skip]=${this.state.skip}`
  }

  handleClick() {
    this.setState({skip: this.state.skip + 1})
  }

  render() {
    return (
      <div>
        <a href={this.urlParams()}>Example link</a>
        <pre>{this.urlParams()}</pre>
        <button onClick={this.handleClick}>Change link</button>
      </div>
    )
  }
}


ReactDOM.render(<Example/>, document.querySelector('div#my-example' ))
like image 351
Jon Avatar asked Apr 19 '26 19:04

Jon


2 Answers

This happens when you try to render an object instead of JSX. Making my best educated guess, I think this line is the problem:

let filtered=this.state.data.filter((item)=>{
      return item.companyName.indexOf(keyword) > -1
    });

Filtered appears to be an array of objects, not JSX, so then in the render method:

{this.state.filtered.length === 0 ? dataRender : this.state.filtered}

potentially tries to render filtered objects, not JSX.

To fix this, try adding this:

const filterRender=this.state.filtered.map((dataItem)=>(
  <Table.Row key={dataItem.id}>
  <Table.Cell>{dataItem.companyName}</Table.Cell>
  <Table.Cell>{dataItem.primaryPhone}</Table.Cell>
  <Table.Cell>{dataItem.emailAddress}</Table.Cell>
  <Table.Cell>{dataItem.venueCode}</Table.Cell>
  <Table.Cell>{dataItem.account}</Table.Cell>
  <Table.Cell>{dataItem.openDate}</Table.Cell>
  <Table.Cell>{dataItem.website}</Table.Cell>
  <Table.Cell>{dataItem.description}</Table.Cell>
</Table.Row>
))

and changing this to:

{this.state.filtered.length === 0 ? dataRender : filterRender}

As @jsdeveloper pointed out below, it would be a good idea to make a renderRow method to handle this.

like image 87
Asleepace Avatar answered Apr 22 '26 10:04

Asleepace


The only thing I would add to asleepace answer would be that you should create a function to map to a dataitem:

getDataItems(data) {
    return data.map((dataItem)=>(
      <Table.Row key={dataItem.id}>
      <Table.Cell>{dataItem.companyName}</Table.Cell>
      <Table.Cell>{dataItem.primaryPhone}</Table.Cell>
      <Table.Cell>{dataItem.emailAddress}</Table.Cell>
      <Table.Cell>{dataItem.venueCode}</Table.Cell>
      <Table.Cell>{dataItem.account}</Table.Cell>
      <Table.Cell>{dataItem.openDate}</Table.Cell>
      <Table.Cell>{dataItem.website}</Table.Cell>
      <Table.Cell>{dataItem.description}</Table.Cell>
    </Table.Row>
    ))
}
render() {
  const filteredItems = getDataItems(this.state.filtered)
  const dataItems = getDataItems(this.state.data)
  ...
like image 39
jsdeveloper Avatar answered Apr 22 '26 09:04

jsdeveloper