Objects are not valid as a React child (found: object with keys {})

Ivan

I am sending through socket.io an object to a react application like this:

function getApiAndEmit(socket, quote) 
{
    try 
    {
        var { ticker, type, price, size, timestamp } = quote;
        socket.emit("FromAPI", quote);
        //console.log(ticker, type, price, size, timestamp)
    } 
    catch (error) 
    {
        console.error(`Error: ${error.code}`);
    }
};

When I try to render it on the client side, I get the error:

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.

What is the correct way to render the fields on the web browser with react?

The code

import React, { Component } from "react";
import socketIOClient from "socket.io-client";

class App extends Component {
  constructor() {
    super();
    this.state = {
      response: {},
      endpoint: "http://127.0.0.1:4001"
    };
  }

componentDidMount() {
    const { endpoint } = this.state;
    const socket = socketIOClient(endpoint);
    socket.on("FromAPI", data =>  this.setState({ response: data }));
  }

render() {
    const { response } = this.state;
    return (
      <div style={{ textAlign: "center" }}>
        {response
          ? <ul>
              Quote: {response}
            </ul>
          : <p>Loading...</p>}
      </div>
    );
  }
}

export default App;
lux

React can't render objects. To bypass the error immediately, put in the following:

{
    response
        ? <ul><li>Quote: {JSON.stringify(response)}</li></ul>
        : <p>Loading...</p>
}

If response is an array of Strings:

<ul>Quote: { response.map((item, index) => (<li key={index}>{item}</li>)) }</ul>

If response is an array of objects:

<ul>Quote: { response.map((item, index) => (<li key={index}>{item.quote}</li>)) }</ul>

If response is an object:

<ul><li>Quote: {response.quote}</li></ul>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Objects are not valid as a React child (found: [object Promise])

React native - objects are not valid as a React child (found: object with keys {$$typeof, type, key, ref, props, _owner, _store})

Objects are not valid as a React child (found: object with keys {items}). If you meant to render a collection of children, use an array instead

Objects are not valid as a React child (found: object with keys {children}): ReactJS

Objects are not valid as a React child ( found object: object with keys ($$typeof,type,key,ref,props,_owner,_store})

Error: Objects are not valid as a React child (found: object with keys..........)

Objects are not valid as a React child (found: [object HTMLDivElement])

Objects are not valid as a react child error. Found object with keys()

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

Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {$$typeof, render})

How to fix 'Objects are not valid as a React child (found: object with keys )'

How to fix "Objects are not valid as a React child (found: object with keys {})

Objects are not valid as a React Child (found: object with keys{id, name})

Objects are not valid as a React child (found: object with keys {weight}). If you meant to render a collection of children, use an array instead

Objects are not valid as a React child (found: object with keys {_id, date, createdAt, updatedAt, __v})

Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead

Error: Objects are not valid as a React child (found: object with keys {}). use an array instead

How to fix: Violation: Objects are not valid as a React child (found: object with keys

React , Error: Objects are not valid as a React child (found: object with keys {data})

Objects are not valid as a React child - found: object with keys

Objects are not valid as a React child (found: object with keys {children})

Error: Objects are not valid as a React child (found: object with keys {zip})

Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead

Error : Objects are not valid as a React child (found: object with keys {user})

Error: Objects are not valid as a React child (found: object with keys

Trying to add an array of objects using react useState but getting the error 'Objects are not valid as a React child (found: object with keys {})'

React treating functional component as an object {Uncaught Error: Objects are not valid as a React child (found: object with keys)

react error: Error: Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children,

React Native: Error in asyc component - Objects are not valid as a React child (found: object with keys {_h, _i, _j, _k})