1

This is my code:

class App extends Component {
  constructor() {
    super();
    this.state = {
      data: [
        {
          name: 'Jal Irani',
          imgURL: './man.jpg',
          hobbyList: ['Front-end Web Development', 'Back-end Web Development', 'Eating Free Food'],
        },
      ],
    };
  }
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to Profiler</h1>
        </header>
        <p className="App-intro">//access the json data here</p>
      </div>
    );
  }
}

I want to access the json element in

<p className="App-intro">//access the json data here. 

I have tried to use react map function without success.

Is there any help. AM new in react js.

3 Answers 3

1
import React from "react";
class App extends React.Component {
  constructor() {
    super();
    this.state = {
      data: [
        {
          name: "Jal Irani",
          imgURL: "./man.jpg",
          hobbyList: [
            "Front-end Web Development",
            "Back-end Web Development",
            "Eating Free Food"
          ]
        }
      ]
    };
  }

  render() {
    const { name, imgURL, hobbyList } = this.state.data[0];
    console.log(name);
    console.log(imgURL);
    console.log(hobbyList);
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to Profiler</h1>
        </header>
        <p className="App-intro" />
      </div>
    );
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

glad I could help.
1

Did you tried to map over your state data object?

render() {
 return (
  <div className="App">
    <header className="App-header">
      <img src={logo} className="App-logo" alt="logo" />
      <h1 className="App-title">Welcome to Profiler</h1>
    </header>

 {this.state.data.map((dataItem, i) => 
    <div key={i}>
      <span>{dataItem.name}</span>
      <img src={dataItem.imgURL}/>
      <ul>
        {dataItem.hobbyList.map((item, i) => <li key={i}>{item}</li>)}
      </ul>
    </div>
  )}
  </div>
);
}

Comments

1

You can render the data like this:

<p className="App-intro">
  <span>{this.state.data[0].name}</span>
  {this.state.data[0].hobbyList.map(e => <span key={e}>{e}</span>)}
</p>

5 Comments

don't forget to add a key to span
Am getting an error that 'e' is not defined no-undef
Missed a closing bracket after </span>. Try the updated code again.
don't use whole object as a key, use index: .map((e, i) => <span key={i}>{e}</span>)
@IgorAlemasow e is a string, not an object. Also using index as key is discouraged: reactjs.org/docs/lists-and-keys.html#keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.