Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda