Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
map an array in componentDidMount and render

i'm traying to map an array in componentDidMount and render it but it doesn't show anything can anyone help me please?

componentDidMount() {
  baseUrl
      .get("/api/v1/bases")
        .then((response) => {
          const data = response.data.data.data.data;
          console.log(data)
             const items = data.map((item) => {
               return (
                 <div>
                   <tr key={item.id}>
                     <td scope="row">{item.id}</td>
                     <td scope="row">{item.name}</td>
                     <td scope="row">
                       <button className="btn btn btn-outline-danger m-1 my-2 my-sm-0">
                         حذف
                       </button>
                       <Link
                         id={item.id}
                         to={`/Bases/edit/${item.id}`}
                         className="btn btn btn-outline-warning m-1 my-2 my-sm-0"
                       >
                         ویرایش
                       </Link>
                     </td>
                   </tr>
                 </div>
               );
             });
          })
      .catch((err) => {
        console.log(err);
      });
}

and i am trying to render it like this code inside table

</tr>
          {this.items}
        </tbody>

it is just part of code

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

componentDidMount / useEffect are used to hook into the component's lifecycle. They are not meant to render/return any JSX elements. You could transform/map your data within componentDidMount and update the local component state which can then be used in render to display your data

class Component {
  state = [];

  async componentDidMount() {
    const items = await fetch()

    const mappedItems = items.map(item => item)

    this.setState(items)
  }

  render() {
    <ul>{this.state.map(item => (
      <li key={item.id}>
        {item}
      </li>
    ))}
    </ul>

  }
}
about 4 years ago · Juan Pablo Isaza Report

0

Just call your API in ComponenDidMount lifecycle and display your data into render function

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!