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

125
Visualizações
Passing props between siblings

i want to share props from components to sibling children. I've read about React Context but can't implement it.

My home component looks like this:

const Home = () => {
    return ( 
        <>
            <Navigation />
            <SearchBar />
            <Wrapper>
                <Filters />
                <ProductsList />
            </Wrapper>
        </>
     );
}

I have search state in SearchBar component, and need to pass it to ProductList component

const [search, setSearch] = useState('');

const handleSetSearch = (e) => {
    setSearch(e.target.value);
}

return ( 
    <Wrapper>
        <StyledTitle>inPal Search</StyledTitle>
        <InputWrapper>
            <StyledInput type="text" placeholder="Write something..." onChange={(e) => handleSetSearch(e)} />
            <SearchIcon src={searchIcon} alt="Search" />
        </InputWrapper>
    </Wrapper>
 );

Can someone help me to understand this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can declare the state in parent component (Home) and pass it as prop to both the child components as:

const Home = () => {

const [search, setSearch] = useState('');

return ( 
    <>
        <Navigation />
        <SearchBar search={search} setSearch={setSearch} />
        <Wrapper>
            <Filters />
            <ProductsList search={search} />
        </Wrapper>
    </>
 );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

import React, { createContext } from "react";
import ProductsList from "./ProductsList";


const [search, setSearch] = useState('');

const handleSetSearch = (e) => {
    setSearch(e.target.value);
}
const Sr = createContext();


return ( 
    <Wrapper>
        <StyledTitle>inPal Search</StyledTitle>
        <InputWrapper>
            <StyledInput type="text" placeholder="Write something..." onChange={(e) => handleSetSearch(e)} />
            <SearchIcon src={searchIcon} alt="Search" />
        </InputWrapper>
<Sr.Provider value={search}>   
<ProductsList/>
</Sr.Provider>
 </Wrapper>
 );

ProductList
import React, { useContext } from "react";
import { Sr } from "./App";
const ProductList = () => {
    const sr = useContext(Sr);
    return <h1> your value is here {sr} </h1>
}
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