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

195
Visualizações
How to get value from one component to another page/component without navigation?

I have a navbar component in which there is an input search bar. Currently I am taking the value of the search bar and navigate to the Results component and access the input value useParams.

I have the let [ result, setResult ] = useState([]); in my Results component because the results can change after the search is entered with buttons on the page. The problem is that I cannot set the initial result while defining the useState because I am fetching from an API.

So every time I render, I first get an empty array and failed promise, after which I get the desired one. How to fix this? I need the search bar to be in the navbar.

This is the code. New to React.

const Navbar = () => {

    let navigate = useNavigate();
    const handleKeyDown = (event) => {
        if (event.key === 'Enter') {
            let value = event.target.value;
            navigate(`/results/${value}`); 
        }
    }
    
    return (
        <nav className='navigation'>
            <div className='left-slot'>
                <button>runtime</button>
            </div>

            <div className="middle-slot">
                <input className="after" 
                placeholder="get runtimes" onKeyDown={handleKeyDown}>
                </input>
            </div>

            <div className='right-slot'>
                <button>How It Works</button>
                <button>Coming Soon</button>
            </div>
        </nav>
    );
}
const Results = () => {

    let { value } = useParams();
    let [ result, setResult ] = useState();

    useEffect(() => {
        fetchArrayByPage(value, page, option).then(res => setResult(res))
    }, [value])

    console.log(value);
    console.log(result);
    
    return (<div></div>)

}

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

0

try a wrapping function for fetching and setting. i would suggest something like this:

async function handleResults(){
    const res = await fetchArrayByPage(value, page, option)
    setResult(res)
}

then you can call it inside useEffect

about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not entirely sure why your code does not work, so I'll provide three options.

Option 1 - If your problem is value is undefined.

Change your useEffect in Results to this:

useEffect(() => {
  fetchArrayByPage(value && value.length ? value : '', page, option).then(res => setResult(res))
}, [value]);

Option 2 - If you need to pass props and Navbar and Results are not on separate routes.

Just pass value as props from Navbar to Results.

Option 3 - Passing components without props.

Use the Context API. This enables you to share data across components without needing to manually pass props down from parent to child.

  1. Initialize variables in context.
  • Create separate file containing context.
import React, { createContext } from 'react';

const NavbarContext = createContext(null);

export default NavbarContext;
  • Import said context to App.js or App.tsx if you're using Typescript.
  • Declare variables and store them in an object for later reference.
const [value, setValue] = useState('');
...

const variables = {
    value,
    setValue,
    ...,
};
  • Wrap with Provider. Pass context variables to the Provider, enabling components to consume variables.
return (
  <NavbarContext.Provider value={variables}>
    ...
  </NavbarContext.Provider>
);
  1. Import and use all your variables in Navbar and Results.
const { value, setValue, ... } = useContext(NavbarContext);
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