简体   繁体   English

如何基于 state 渲染组件

[英]How do I render component based on state

I've got this filter Icon from MaterialUI and I need it to be filled black (for now it can change position) when a checkmark is checked.我从Metteralui中获得了此过滤器图标,当检查检查标记时,我需要将其填充为黑色(现在可以更改位置)。 When a box is checked setFilters is filled.当一个框被选中时,setFilters 被填充。 So if a check is Product Type is ticked, setFilters(prt:[]) wont be empty.因此,如果勾选了产品类型,则 setFilters(prt:[]) 不会为空。

How do I make it so that if setFilters(prt:[]) is not empty, I can render the filter icon with a different position (eventually i'll render a different icon).如何做到这一点,以便如果 setFilters(prt:[]) 不为空,我可以使用不同的 position 呈现过滤器图标(最终我将呈现不同的图标)。

I've already tried我已经试过了

 { props.filters > 0 && d.autoFilter? <FiFilter style={{position:"relative", top:"4px", left: "12px"}} onClick={() => props.toggleVisibleFilter(d.field)}/>:<div/>}

 // component A const [filters, setFilters] = useState({search:'',prt:[],cnt:[],cat:[]}) // Component B const headerFields = [ {field: 'ID', label: '', width: '5%'}, {field: 'bnd', label: 'Brand', width: '15%' }, {field: 'cnt', label: 'Country', autoFilter: true, width: '15%' }, {field: 'prt', label: 'Product type', autoFilter: true, width: '20%' }, {field: 'cat', label: 'Category', autoFilter: true, width: '20%' }, {field: 'url', label: 'URL', width: '25%' }, ] // JSX <TableRow> {headerFields.map((d)=> <TableCell classes={{ root:classes.root }} style={{ textAlign:"left",fontSize:"18px", position:"sticky", width:d.width, background:"#F28808"}} align="left">{d.label} { d.autoFilter? <FiFilter style={{position:"relative", top:"4px", left: "4px"}} onClick={() => props.toggleVisibleFilter(d.field)}/>:<div/>} {props.visibleFilter===d.field?<MultiSelectFIlter filterHandler={props.filterHandler} API_DATA={props.API_DATA} visibleFilter={props.visibleFilter} setVisibleFilter={props.setVisibleFilter} setFilters={props.setFilters} filters={props.filters} />:(null)} </TableCell> )} </TableRow>

在此处输入图像描述

You can use conditional approach like this您可以使用这样的条件方法

style={`${filters==[]} ? {display:"none"} : {display:"block"} `}

From your conditional above, you have an error.从上面的条件来看,您有一个错误。 For when you check if the array is empty or not you must check the length of it not just props.filters but instead props.filters.length in order for the conditional to work, otherwise while array is defined and the length is 0 the props.filters will always return true;因为当您检查数组是否为空时,您必须检查它的长度,而不仅仅是props.filters而是props.filters.length以使条件起作用,否则当定义数组并且长度为 0 时, props.filters将始终返回 true;

also since in your case you have defined it like:还因为在您的情况下,您已将其定义为:

 const [filters, setFilters] = useState({search:'',prt:[],cnt:[],cat:[]})

your conditional should be something like this:你的条件应该是这样的:

{ props.filters.prt.length > 0 ? ...

暂无
暂无

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

相关问题 如何仅在设置 state 后渲染反应组件? - How do I render a react component only after a state is set? 如何根据特定页面有条件地呈现 Gatsby 中的组件? - How do I conditionally render a component in Gatsby based on specific page? Vue:如何根据父数据渲染动态组件? - Vue: How do I render a dynamic component based on parent data? 如何根据 prop 有条件地渲染组件的颜色? - How do I conditionally render the color of my component based on the prop? 如何在组件渲染之前基于 localstorage 操作组件 state - how to manipulate component state based on localstorage before component render 我想将状态从一个组件发送到另一个组件来渲染它,没有 Redux 怎么办? - I want to send the state from a component to another component to render it, how to do it without Redux? 如何根据 state 更改 className 并在功能组件中重新渲染它 - How to change className based on state and re render it in a functional component 如何根据状态变化重新渲染组件 - How to re-render a component based on state changing React - 当父状态组件发生更改时,如何强制子组件重新呈现? - React - How do i force child components to re render when parent state component changes? 我该如何渲染 <div> 基于屏幕大小使用React JS中的全局变量而不使用状态? - How do I render a <div> based on the screen size using a global variable in React JS without using a state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM