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

183
Visualizações
How do you save an object from an API to a "favorites" component in React?

I'm using a NASA API to render media images and info on a page. I want to add a button that "saves" the image to a favorites array. How would I do this? Right now, I have it so that when a button is clicked, the whole array is duplicated instead of a single object from the API.

import React, { useState, useEffect } from "react";

const apiKey = process.env.REACT_APP_APOD_KEY;

function Main() {

    const [media, setMedia] = useState([]);
    const [faves, setFaves] = useState([]);

    const mediaGet = () => {
        fetch(`https://api.nasa.gov/planetary/apod?api_key=${apiKey}&count=10`)
            .then(res => res.json())
            .then(result => {
                setMedia(result)
            })
    }

    const addFave = (media) => {
        const newFavesList = [...faves, media];
        setFaves(newFavesList);

        // make this function add to faves array (new array)

    };

    useEffect(() => {
        mediaGet()
    }, [])

    console.log(media);


    return (
        <>
            <h1>Clever name here</h1>

            {/* move this to Card component */}

            {media.map((media) => (
                <div key={media.id}>
                    <h2>{media.title}</h2>
                    <h3>{media.date}</h3>
                    <img src={media.url} alt={media.title} />
                    <button onClick={addFave} type="button">Add to array</button>
                </div>
            ))}

        </>
    )
}

export default Main;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your addFave function requires an argument (media) but you are calling it with onClick={addFave} which will only pass the click Event when called.

To pass the media object from your map callback, use

onClick={() => addFave(media)}

I would also change the handler to use the functional version of the useState setter

const addFave = (fave) => {
  setFaves(prev => [...prev, fave])
}

FYI, try to avoid shadowing (re-using variable names in different scopes). There's even a handy ES-lint rule for this ~ https://eslint.org/docs/rules/no-shadow

{media.map(item => (
  <div key={item.id}>
    <h2>{item.title}</h2>
    <h3>{item.date}</h3>
    <img src={item.url} alt={item.title} />
    <button onClick={() => addFave(item)} type="button">Add to array</button>
  </div>
))}
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