Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React is rendering [object object] rather than the JSX

Tags:

I'm trying to render out journal entries on my site with an object (not array) and I am running into an issue, here is my current code

  populateJournal(){ const j = Object.values(this.state.journal); var journalEntries = '';    for (var i = 0; i < j.length; i++){     journalEntries+=       <div>       <h3>{j[i].title} - {j[i].date}</h3>       <p>{j[i].entry}</p>       </div>;    }   return(<div>{journalEntries}</div>); 

}

When i call this function it renders "<div>[object object]</div>" and the text between the divs is plain text.

When i change the loop to say "journalEntries = <div...." it renders out the last journal entry as expected, but the issue is that it's not actually appending the journal entries with the loop.

ideas?

like image 357
ELI7VH Avatar asked Jun 12 '17 05:06

ELI7VH


People also ask

What is the difference between JSX and React?

JSX is just a syntax extension of JavaScript which allows users to write React components; React is an open source frontend JavaScript library for building complex UIs; it allows the programmers to create complex UIs from smaller components.

What is alternative to JSX in React?

React, ES6, JavaScript, EJS, and Python are the most popular alternatives and competitors to JSX.

What is rendering in React?

React renders HTML to the web page by using a function called render(). The purpose of the function is to display the specified HTML code inside the specified HTML element. In the render() method, we can read props and state and return our JSX code to the root component of our app.

Is it mandatory to use JSX in React?

JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React.

What is an element in react rendering?

Rendering Elements. Elements are the smallest building blocks of React apps. An element describes what you want to see on the screen: const element = <h1>Hello, world</h1>; Unlike browser DOM elements, React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements. Note:

What is JSX&why should we use it in react?

If you’re new to React, you’ll likely have heard about JSX, or JavaScript XML — it’s an XML-like code for elements and components. In this article, we’re going to take a look at what JSX is & why we should use it in our React applications. We’ll also take a look at what elements are, and how we can render them to the DOM.

What is an object in react?

This object is known as a React element, and it functions a lot like a description of what you see on the screen. React uses these objects to build the DOM and keep it up to date.

What is actually going on with JSX when we render components?

Let’s embed the result of a called JavaScript function: Hello, {printName (user)}! So whats actually going on with JSX, when we render components? Each element being rendered by the Greeting component are transpiled down into React.createElement calls.


1 Answers

Inspead of a defining journalEntries as a string define it as an array and push the JSX elements to the array in order to render like

populateJournal(){      const j = Object.values(this.state.journal);     var journalEntries = [];        for (var i = 0; i < j.length; i++){         journalEntries.push(           <div>           <h3>{j[i].title} - {j[i].date}</h3>           <p>{j[i].entry}</p>           </div>);        }       return(<div>{journalEntries}</div>);  } 

When you append to the String, you are not actually appending a string but an object which is incorrect and hence you get [Object Object]

You can also use map to render your context. See this answer on how to use map:

REACT JS: Data Display and Array manipulation

like image 161
Shubham Khatri Avatar answered Oct 05 '22 07:10

Shubham Khatri