[英]React how can i change my component with click
I have a component ( Card ) and it have a icon in right-up corner.我有一个组件( Card ),它在右上角有一个图标。 When i click that icon i want to it change to another component ( EditCard ) for edit it.
当我单击该图标时,我希望将其更改为另一个组件( EditCard )以进行编辑。 That Card components is coming with mapping on my datas.
Card 组件随附在我的数据上的映射。 And when i set state for change it like :
当我设置状态进行更改时:
const [editVisible, seteditVisible] = useState(false);
.
.
.
{datax &&
datax.map((x) => {
return (
<div key={x._id}>
{editVisible === false && (
<div>
<div className="flex">
<Link
className="hover:text-current w-full "
to={{
pathname: `${APP_PREFIX_PATH}/collections/${x._id}`,
}}
>
<Card data={x} />
<br />
</Link>
<span
className="ti-pencil-alt relative right-8 top-2 text-lg"
onClick={() => {
seteditVisible(true);
}}
></span>
</div>
</div>
)}
{editVisible === true && (
<div>
<div className="flex">
<div className="w-full">
<EditCard data={x} />
</div>
</div>
</div>
)}
</div>
);
})}
Yes its working and changing component, but its changing for all of my Cards.是的,它的工作和变化组件,但它对我所有的卡片都在变化。 I want to change only which i click.
我只想更改我单击的内容。 How can i make it ?
我怎样才能做到? For make it more clear Screenshots :
为了让它更清晰截图:
My Cards : https://prnt.sc/1artutf我的卡片: https : //prnt.sc/1artutf
When i click right-top corner icon ( One of card icon only ) : https://prnt.sc/1arv7f7当我单击右上角的图标(仅限卡片图标之一)时: https : //prnt.sc/1arv7f7
Instead of seteditVisible(true) in your onClick, what you can do is pass x._id.您可以做的是传递 x._id,而不是在您的 onClick 中使用 seteditVisible(true)。
onClick={() => seteditVisible(x._id)}
and in your render you can在你的渲染中你可以
{datax && datax.map((x) => {
return (
<div key={x._id}>
{editVisible === x._id && (
<div>
<div className="flex">
...
...
...
)}
{editVisible !== x._id && (
<div>
...
...
</div>
)}
)}
)};
you can further simply with a ternary block.您可以进一步简单地使用三元块。
{editVisible === x._id ? (
<>
JSX
</>
) : (
<>
JSX
</>
)}
I think you want to create another state like const [flipStatus, setFlipStatus] = useState([dataX.map((element) => false)}])
to hold a value for each element in the array and have a handleClick function like so我想你想创建另一个状态,比如
const [flipStatus, setFlipStatus] = useState([dataX.map((element) => false)}])
来保存数组中每个元素的值,并有一个像这样的 handleClick 函数
const handleClick = (index) => {
const newflipStatus = [...flipStatus]
newflipStatus[index] = !flipStatus[index]
setFlipStatus(newflipStatus);
}
and have that flippedStatus on the map function FlipStatus[index] === true && ...
I think this could be a way of start thinking for a solution, have a look at this answer also并在地图函数
FlipStatus[index] === true && ...
我认为这可能是开始思考解决方案的一种方式,也看看这个答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.