Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
Trigger parent API call from child in react

In my parent(Feed) I display a list of posts:

function Feed() {

    const [posts, setPosts] = useState(null);

    useEffect(() => {
        const fetchPosts = async () => {
            try {
                const result = await axios({
                ...
                });
    
                setPosts(result.data);
                ...
            } catch (e) {
                ...
            }
        };
        fetchPosts();
    }, []);

    return (
        ...
            {posts &&
                <div>
                    <PostCompose />                              // post creation
                    {posts.map((item, index) =>                  // map existing posts
                        <Post key={item._id} {...item} />)}      
                </div>
            }
        ...
    )
}

The child(PostCompose) can create a post:

function PostCompose() {

    const [text, setText] = useState('');

    const createPost = async () => {
        try {
            const result = await axios({
                ...
            })
            ...
        } catch (e) {
            ...
        }
    }

    const handleTextChange = (e) => {
        setText(e.target.value);
    }

    return (
        ...
        <textarea onChange={handleTextChange} />
        <button onClick={createPost}>Post</button>
        ...
    )
}

The PostCompose component makes the requests to the backend to create the new posts, but how do I trigger the Feed to refresh to pull the new list of posts?

I feel like my approach might need some work, any help would be great as I'm very new to React. Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

One way would be to take your fetchPosts function out of the effect callback, using the useCallback hook to memoize it. You can now invoke fetchPosts when the Feed component mounts and provide it to your PostCompose component, as I've done here with the afterPostCreated prop:

function Feed() {
    const [posts, setPosts] = useState(null);

    const fetchPosts = useCallback(async () => {
        try {
            const result = await axios({
                // ...
            });

            setPosts(result.data);
            // ...
        } catch (e) {
            // ...
        }
    }, []);

    useEffect(() => {
        fetchPosts();
    }, [fetchPosts]);

    return (
        posts && (
            <div>
                <PostCompose afterPostCreated={fetchPosts} /> // post creation
                {posts.map(
                    (
                        item,
                        index // map existing posts
                    ) => (
                        <Post key={item._id} {...item} />
                    )
                )}
            </div>
        )
    );
}

Then simply invoke that callback in your PostCompose component, after the post was successfully created:

function PostCompose({ afterPostCreated }) {
    const [text, setText] = useState('');

    const createPost = async () => {
        try {
            const result = await axios({
                // ...
            });
            // ...

            if (afterPostCreated) {
                afterPostCreated();
            }
        } catch (e) {
            // ...
        }
    };

    const handleTextChange = (e) => {
        setText(e.target.value);
    };

    return (
        <>
            <textarea onChange={handleTextChange} />
            <button onClick={createPost}>Post</button>
        </>
    );
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda