![](/img/trans.png)
[英]React Hooks useState changing multiple elements state in one mouseOver function only want to change the moused over one
[英]React/React Hooks: onChange function to change text is changing for all 3 elements simultaneously instead of just one
每当用户点击打开它时,我都有一个使用反应挂钩的组件来更改样式化折叠/手风琴面板的文本。 我遇到的问题是这个逻辑同时影响所有3个折叠面板的文本,而不仅仅是打开的面板。 我已经包含了一个代码沙箱的链接来突出显示行为,我已经包含了下面的代码
Code Sandbox
https://codesandbox.io/s/q-56761334-style-collapse-extra-bdcbz
import React, { useState } from "react";
import ReactDOM from "react-dom";
import { Col, Row, Collapse } from "antd";
import styled from "styled-components";
import "antd/dist/antd.css";
const Flexbox = styled.div`
font-family: sans-serif;
flex-direction: column;
display: flex;
justify-content: center;
border: solid 1px palevioletred;
padding: 10%;
margin: 10%;
`;
const StyledCollapse = styled(Collapse)`
&&& {
border: none;
border-radius: 0px;
background-color: #f7f7f7;
box-shadow: none;
}
`;
const StyledH1 = styled.h1`
font-weight: 700;
`;
function FromValidate() {
const [disabled, setDisabled] = useState(true);
return (
<Flexbox>
<Row>
<Col span={12}>
<StyledCollapse onChange={() => setDisabled(prev => !prev)}>
<Collapse.Panel
header="DROPDOWN EXAMPLE"
key="1"
showArrow={false}
bordered={false}
extra={<p>{disabled ? "SHOW" : "HIDE"}</p>}
>
<div>
<StyledH1>Placeholder</StyledH1>
</div>
</Collapse.Panel>
</StyledCollapse>
<StyledCollapse onChange={() => setDisabled(prev => !prev)}>
<Collapse.Panel
header="DROPDOWN EXAMPLE"
key="1"
showArrow={false}
bordered={false}
extra={<p>{disabled ? "SHOW" : "HIDE"}</p>}
>
<div>
<StyledH1>Placeholder</StyledH1>
</div>
</Collapse.Panel>
</StyledCollapse>
</Col>
</Row>
</Flexbox>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<FromValidate />, rootElement);
import React, { useState } from "react"; import ReactDOM from "react-dom"; import { Col, Row, Collapse } from "antd"; import styled from "styled-components"; import "antd/dist/antd.css"; const Flexbox = styled.div` font-family: sans-serif; flex-direction: column; display: flex; justify-content: center; border: solid 1px palevioletred; padding: 10%; margin: 10%; `; const StyledCollapse = styled(Collapse)` &&& { border: none; border-radius: 0px; background-color: #f7f7f7; box-shadow: none; } `; const StyledH1 = styled.h1` font-weight: 700; `; function FromValidate() { const [disabled, setDisabled] = useState(true); const [disabled1, setDisabled1] = useState(true); return ( <Flexbox> <Row> <Col span={12}> <StyledCollapse onChange={() => setDisabled(prev => !prev)}> <Collapse.Panel header="DROPDOWN EXAMPLE" key="1" showArrow={false} bordered={false} extra={<p>{disabled ? "SHOW" : "HIDE"}</p>} > <div> <StyledH1>Placeholder</StyledH1> </div> </Collapse.Panel> </StyledCollapse> <StyledCollapse onChange={() => setDisabled1(prev => !prev)}> <Collapse.Panel header="DROPDOWN EXAMPLE" key="1" showArrow={false} bordered={false} extra={<p>{disabled1 ? "SHOW" : "HIDE"}</p>} > <div> <StyledH1>Placeholder</StyledH1> </div> </Collapse.Panel> </StyledCollapse> </Col> </Row> </Flexbox> ); } const rootElement = document.getElementById("root"); ReactDOM.render(<FromValidate />, rootElement);
或者您可以创建这样的自定义组件:
import React, { useState } from "react"; import ReactDOM from "react-dom"; import { Col, Row, Collapse } from "antd"; import styled from "styled-components"; import "antd/dist/antd.css"; const Flexbox = styled.div` font-family: sans-serif; flex-direction: column; display: flex; justify-content: center; border: solid 1px palevioletred; padding: 10%; margin: 10%; `; const StyledCollapse = styled(Collapse)` &&& { border: none; border-radius: 0px; background-color: #f7f7f7; box-shadow: none; } `; const StyledH1 = styled.h1` font-weight: 700; `; function FromValidate() { return ( <Flexbox> <Row> <Col span={12}> <Customcollapse header="example1" /> <Customcollapse header="example2" /> </Col> </Row> </Flexbox> ); } const Customcollapse = props => { const [disabled, setDisabled] = useState(true); return ( <StyledCollapse onChange={() => setDisabled(prev => !prev)}> <Collapse.Panel header={props.header} key="1" showArrow={false} bordered={false} extra={<p>{disabled ? "SHOW" : "HIDE"}</p>} > <div> <StyledH1>Placeholder</StyledH1> </div> </Collapse.Panel> </StyledCollapse> ); }; const rootElement = document.getElementById("root"); ReactDOM.render(<FromValidate />, rootElement);
这是因为您在所有元素中使用相同的disabled
变量。 您需要为每个元素分别disabled
(ex disabledA
, disabledB
)变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.