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

166
Views
Pushing array with useState()

Everyone! i just have this kind of problem that i can`t fix.

this is my App.js

import { useState } from "react"
import Header from "./components/header"
import FeedbackList from "./components/FeedbackList"
import FeedbackData from "./data/FeedbackData"

function App() {

    const [feedback, setFeedback] = useState(FeedbackData)

    return (
        <>
        <Header />
        <div className="container">
            <FeedbackList feedback={feedback} />
        </div>
        </>
    ) }

    
    export default App

and this is my second js file that i want to use "feedback" prop like array

import FeedbackItem from "./FeedbackItem"
import FeedbackData from "../data/FeedbackData"
function FeedbackList(feedback) {

return (
    <div>
        {feedback.map((item)=>(
            <FeedbackItem key={item.id} item={item}/>
        ))}
    </div>
  )
}

i cant use feedback.map function in this case because feedback is not like array (sorry for my bad english) i solve this problem without using hooks but i want to know what i can do in this case,sorry if im writing something wrong im just new in React and trying to learn.

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

0

In Javascript there are object and array. Both can be mapped. For Array.

const arr = [
  { name: "name 1", id: "01" },
  { name: "name 2", id: "02" },
  { name: "name 3", id: "03" },
];

arr.map(item=> (<div key={item.id}>{item.name}</div>))

For Object.

const obj = {
  "item01": { name: "name 1" },
  "item02": { name: "name 1" },
  "item03": { name: "name 1" },
};

Object.keys(obj).map((key)=> <div key={key}>{obj[key].name}</div>)

Check your type, console.log(typeof FeedbackData).

If it is not type error. Instead of

const [feedback, setFeedback] = useState(FeedbackData)

Try this.

const [feedback,setFeedback] = useState([])
useEffect(()=> setFeedback(FeedbackData),[])
about 4 years ago · Juan Pablo Isaza Report

0

what you want is to destructure the prop you need from the 'props' object.

function Component1() {
  const [val, setVal] = useState([]);
  
  return <Component2 val={val} />
}

function Component2({ val }) {
  return val.map...
}

this is equivalent to doing:

function Component2(props) {
  return props.val.map...
}

this is because props is an object, and so you need to get the right key from the object based on the prop name, either by destructuring or accessing it via props.propName

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!