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

148
Visualizações
React passing input from search component to main App.js

i am trying to build app which is searching Movies database api. I have main fetch function in App.js. In tutorials people using searchbar in this main APP component. Isnt better to use separate Navbar component witch searchbar and then pass that value to the main App where is fetch fuction and change that function based on searched value? is there any way to pass that input value without Redux ? I googled "lifting state up" but i am little bit confused.:) Thank You Main App.js

const [movies, setMovies] = React.useState([]);
const example = [1, 2, 3];
React.useEffect(() => {
  console.log("hehe");
  fetch(
    "https://api.themoviedb.org/3/trending/all/week?api_key=564564564"
  )
    .then((res) => res.json())
    .then((data) => {
      console.log(data);
      setMovies(data.results);
    });
}, []);

return (
  <div>
    <Navbar />
    <div className="container1">
      <div className="movie">
        {movies.map((movieReq) => (
          <Movieinfo key={movieReq.id} {...movieReq} />
        ))}
      </div>
    </div>
  </div>
);
}

export default App;

Here is Navbar with searchbar and input value which i want to pass to App.js

import "./Navbar.css";
function Navbar() {
  return (
    <div>
      <nav class="navbar navbar-light bg-light">
        <a class="navbar-brand">Navbar</a>
        <form class="form-inline">
          <input
            class="form-control mr-sm-2"
            type="search"
            placeholder="Search"
            aria-label="Search"
          />
          <button class="btn btn-outline-success my-2 my-sm-0" type="submit">
            Search
          </button>
        </form>
      </nav>
    </div>
  );
}

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

0

There are two simple ways you can solve your problem.

  1. Composition (Read here)

You can put all your login in app.js and create two other components, main.js, and nav.js. You can pass the state as props to both components.

function App() {

const [movies, setMovies] = useState([]);
const [search, setSearch] = useState([]);


return (
  <>
  
  <Main movies={movies} setMovies={setMovies}/>
  <Navbar search={search} setSearch={setSearch} />
  
  </>
)
}

function Main({movies, setMovie}) {

return (
  <>
 // you can render here
  </>
)
}

function Main({search, setSearch}) {

return (
  <>
 // change the search here
  </>
)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

  1. Context (My personal favorite) This is a very powerful feature, you must try it. Here is an starter setup template that you can use. Check this out
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