Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - How to iterate over dictionary in render method using JSX?

I'm learning React and have set up a small test app that makes an Ajax call that returns a JSON object that I want to iterate over in the return method of my component. I've tried everything I can think of and have googled this, but like an hour later I'm still stumped.

Here is what I have...

render() {
  const { vals } = this.state;
  return (
    <div>
      {/* note that this correctly outputs the value of vals[key]: {vals['key']} */}
      Object.keys({vals}).map((key, index) => ( 
        <p key={index}> this is my key {key} and this is my value {vals[{key}]} </p> 
      ))
    </div>
  )
}

What am I doing wrong here? Any recommendations on a good reference for ES6/JSX? I've been struggling with simple things, with no good way to look up this info.

like image 711
user797963 Avatar asked Apr 11 '17 03:04

user797963


People also ask

What is the use of render () method in ReactJS?

In the render () method, we can read props and state and return our JSX code to the root component of our app. In the render () method, we cannot change the state, and we cannot cause side effects ( such as making an HTTP request to the webserver). Step 1: Create a React application using the following command.

How to iterate list of objects in react render?

It uses es6 arrow function for an call-back for-of loop is new way to iterate objects. As react render returns one element, before returning list, construct list using different approaches mentioned above and return a final array.

How do you iterate through an array in JSX?

Using the map () Method The map () method is the most commonly used function to iterate over an array of data in JSX. You can attach the map () method to the array and pass a callback function that gets called for each iteration. When rendering the User component, pass a unique value to the key prop.

How to iterate a list of employees in react JSX?

In the Parent component, a List of employee are iterated in React JSX using different syntax First, react state object is initialized with an array of employee data. Basic for loop syntax is used to iterate the array, You can check more about for loop


1 Answers

{vals} extracts out the vals property from an object. Hence Object.keys({vals}) is incorrect as vals is already an object. Likewise, it should be {vals[key]} instead of {vals[{key}]}.

render(){
  const {vals} = this.state;  // Essentially does: const vals = this.state.vals;
  return (
    <div>
      {
        Object.keys(vals).map((key, index) => ( 
          <p key={index}> this is my key {key} and this is my value {vals[key]}</p> 
        ))
      }
    </div>
  )
}

If vals is an object containing { a: 1, b: 2}, Object.keys(vals) will get you ['a', 'b'] and in the first iteration of the map, key will be 'a' and to access the value, do vals[key] which is essentially vals['a'] => 1.

I think you are confused by the Object destructuring syntax. It's really quite simple as it's just syntactic sugar over ES5 JavaScript (most ES6 is just sugar). Have a read at MDN's docs on Destructuring assignment to understand it better.

like image 188
Yangshun Tay Avatar answered Oct 10 '22 06:10

Yangshun Tay