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' ))
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.
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)
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With