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

114
Views
How to add a new json object to array which elements will be rendered as components on a page?

I want to change a state of an array which will have values derived from data.js file as a starting point. Changing a state means running function setAllThingsArray and adding new element which will be set using a function setThingsArray based on previous state (thingsArray). Do I need to map allThingsArray to get updated array with added elements and to list them ?

import React from "react";
import data from "../data";
import Stamp from "../components/Stamp";

export default function AddItem(props) {
  
  const [thingsArray, setThingsArray] = React.useState({
    key: 9,
    img: "../images/stamp1.jpg",
    name: "Stamp 5",
    description: "AAAA",
    rating: 78.0,
  });

  function addItem() {
    setAllThingsArray((prevAllThingsArray) => ({
      arr: [
        ...prevAllThingsArray.arr,
        setThingsArray((prevThingsArray) => ({
          ...prevThingsArray,
          key: prevThingsArray.key + 1,
          img: "../images/stamp1.jpg",
          name: "Stamp 5",
          description: "AAAA",
          rating: 78.0,
        })),
      ],
    }));
  }

  const [allThingsArray, setAllThingsArray] = React.useState(data);

  const allThingsElements = allThingsArray.map((st) => {
    return <Stamp key={st.key} st={st} />;
  });

  return (
    <>
      <div className="form">
        <input type="text" placeholder="Image" className="form--input" />
        <input type="text" placeholder="Stamp title" className="form--input" />
        <input type="text" placeholder="Description" className="form--input" />
        <input type="text" placeholder="Rating" className="form--input" />
        {/* <button className="form--button" onClick={addStampToJson(json_stamp)}>
          Add Stamp
        </button> */}
        <button className="form--button" onClick={addItem}>
          Add Stamp
        </button>
      </div>
      <section className="stamps-list">{allThingsElements}</section>
    </>
  );
}


Is it correct to place setThingsArray function inside setAllThingsArray function?

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

0

state setters (setAllThingsArray and setThingsArray) are asynchronous, so you cannot use a state update within another state update. setThingsArray itself is also a function (not a state value), so you cannot set it directly into setAllThingsArray either.

I'd propose that you should add another variable to keep an updated object (not an updated state), and then update states separately with the updated object.

function addItem() {
    //add an updated object
    const updatedThingsArray = {
        ...thingsArray, //this is an actual previous state
        key: prevThingsArray.key + 1,
        img: "../images/stamp1.jpg",
        name: "Stamp 5",
        description: "AAAA",
        rating: 78.0,
    }
    setAllThingsArray((prevAllThingsArray) => ([
        ...prevAllThingsArray,
        updatedThingsArray, //it's become a new object with your updated data
      ]));

    setThingsArray(updatedThingsArray) //update it back to the `thingsArray` as the new state
  }
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!