简体   繁体   English

如何有条件地渲染组件?

[英]How to render a component conditionally?

I have shop.js component where Navbar and ProductList Components are render.I want when someone search then instead of ProductList Component,searchShop component should render.我有 shop.js 组件,其中呈现 Navbar 和 ProductList 组件。我希望当有人搜索而不是 ProductList 组件时,应该呈现 searchShop 组件。

Shop Component商店组件

const Shop = () => {
    return (
        <div className='bb1'>
            <Navbar/>
            <ProductList/>
            <Footer/>
        </div>
    );
};

export default Shop;

Navbar Component导航栏组件

const Navbar = () => {
 const [state,setState]=useState({
        searchText:'',
    })

   
//Some API CALL run only when user search something
return(

 state.searchText?<SearchShop/>:""

)}

Add a function in shop.js where you need to set the visibility of product list and pass the function reference to navbar.在需要设置商品列表可见性的shop.js中添加一个function,将function引用传给navbar。 So when search text is updated call the function.因此,当搜索文本更新时,请拨打 function。

    const Shop = () => {
        const [showProduct, setShowProduct] = useState(true);
        const change_product_display_handler = (state) =>{
            setShowProduct(state)
    }
    return (
        <div className='bb1'>
            <Navbar setProduct={change_product_display_handler}/>
            {showProduct &&<ProductList/>}
            <Footer/>
        </div>
    );
};

export default Shop;

In navbar in function where you got values of search text add the following function在 function 的导航栏中,您可以在其中获得搜索文本的值,添加以下内容 function

const Navbar = () => {
 const [state,setState]=useState({
        searchText:'',
    })
const on_search_handler = () =>{
var searchvalue = //get value here;
//update setstate
 if (searchvalue === '') {
        props.setProduct(true);
    } else {
props.setProduct(false);
}
return(  );
};

export default Navbar ;

But you need to clarify if you need component inside the Navbar, or instead of the ProductList component.但是你需要澄清你是否需要 Navbar 内部的组件,或者而不是ProductList组件。 If you want the ProductList to be replaced, you need to propagate the API call via some kind of state management, and have your logic in the Shop function:如果要替换ProductList ,则需要通过某种 state 管理传播 API 调用,并在 Shop function 中包含您的逻辑:

const Shop = () => {
searchText = getStateFunction(state => state.searchText)

return (
    <div className='bb1'>
        <Navbar/>
      {searchText ? <ProductList/> : <SearchShop/>}
        <Footer/>
    </div>
   );
};

export default Shop;

There are many options for global state, I suggest you go look through the different options, but my absolute favorite to recommend, is Zustand. global state 有很多选项,我建议你 go 查看不同的选项,但我最喜欢推荐的是 Zustand。 It's very easy to both setup, and use.它的设置和使用都非常容易。

According to the React documentation you can do conditional rendering in several ways.根据React 文档,您可以通过多种方式进行条件渲染。 Inline, etc., make sure you are using the the method you are most familiar or comfortable with.内联等,确保您使用的是您最熟悉或最习惯的方法。

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

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