React/React 挂钩:更改文本的 onChange 函数正在同时更改所有 3 个元素,而不仅仅是一个
React/React Hooks: onChange function to change text is changing for all 3 elements simultaneously instead of just one
我有一个组件使用反应钩子来更改样式 collapse/accordion 面板的文本,每当用户单击打开它时。我遇到的问题是此逻辑同时影响所有 3 个折叠面板的文本,而不仅仅是打开的面板。我已将 link 添加到代码沙箱中以突出显示该行为,并且已包含以下代码
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);
这是因为您在所有元素中使用了相同的 disabled
变量。
每个元素需要单独的 disabled
(e.x.disabledA
, disabledB
) 变量。
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);
我有一个组件使用反应钩子来更改样式 collapse/accordion 面板的文本,每当用户单击打开它时。我遇到的问题是此逻辑同时影响所有 3 个折叠面板的文本,而不仅仅是打开的面板。我已将 link 添加到代码沙箱中以突出显示该行为,并且已包含以下代码
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);
这是因为您在所有元素中使用了相同的 disabled
变量。
每个元素需要单独的 disabled
(e.x.disabledA
, disabledB
) 变量。
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);