简体   繁体   English

React 将输入从搜索组件传递到主 App.js

[英]React passing input from search component to main App.js

i am trying to build app which is searching Movies database api.我正在尝试构建正在搜索电影数据库 api 的应用程序。 I have main fetch function in App.js.我在 App.js 中有主要的 fetch 功能。 In tutorials people using searchbar in this main APP component.在教程中,人们在这个主要的 APP 组件中使用搜索栏。 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 ?有没有办法在没有 Redux 的情况下传递该输入值? I googled "lifting state up" but i am little bit confused.:) Thank You Main App.js我用谷歌搜索了“提升状态”,但我有点困惑。:) 谢谢 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这是带有搜索栏和输入值的导航栏,我想将其传递给 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;

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.您可以将所有登录信息放在 app.js 中并创建另外两个组件 main.js 和 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. Context(我个人最喜欢的)这是一个非常强大的功能,你一定要试试。 Here is an starter setup template that you can use.这是您可以使用的入门设置模板。 Check this out 看一下这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM