React/React Hooks:用于更改文本的 onChange 函数同时更改所有 3 个元素,而不是仅更改一个

2024-04-27

我有一个组件,使用反应钩子来更改样式折叠/手风琴面板的文本,每当用户单击打开它时。我遇到的问题是这个逻辑同时影响所有 3 个折叠面板的文本,而不仅仅是打开的面板。我已经包含了一个代码沙箱的链接来突出显示该行为,并且我已经包含了下面的代码

Code Sandbox https://codesandbox.io/s/q-56761334-style-collapse-extra-bdcbz 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);
both collapse have same state that why they are changing at same u need to give each one a state like this!

或者你可以创建一个像这样的自定义组件:

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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React/React Hooks:用于更改文本的 onChange 函数同时更改所有 3 个元素,而不是仅更改一个 的相关文章

  • 在 javascript/jquery 中获取图像的完整尺寸

    我在页面上有一个图像 该图像已调整大小以适合 div 例如 400x300 如何在 jQuery 中获取图像的完整尺寸 4000x3000 width 和 height 似乎只返回图像的当前大小 图像有naturalWidth and na
  • 调整表格上的列宽

    目前 如果表格的宽度不大于容器的宽度 我可以调整表格列的大小 我希望发生的是在调整列大小时表格的宽度增加 以便滚动条出现在表格下方 基本上允许我调整大小而不受容器宽度的限制 这是一个小提琴 https jsfiddle net thatOn
  • antd上传控件需要action函数,但我不需要它

    我正在使用 ant design 组件 并且有一个上传输入 https ant design components upload https ant design components upload 根据文档 需要对道具进行操作 但是 我不
  • 有没有由 HTML + css + javascript 驱动的 sql 编辑器? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 语法高亮 sql代码格式 代码镜像 http codemirror net 会成功的 这太好了 非常容
  • 无法导入@material-ui/core/styles/MuiThemeProvider

    我正在开发一个 React 项目 使用 Material UI React 组件 我想进口MuiThemeProvider in src index js像这样import MuiThemeProvider from material ui
  • 如何防止机器人程序和垃圾邮件 API 请求?

    我正在使用react native 开发一个Android 应用程序 该应用程序与我正在为该应用程序开发的API 进行通信 该 API 是使用 Laravel 和 Laravel Passport 构建的 我知道 Android 应用程序可
  • 当 querySelectorAll 在不使用库的情况下不可用时,按属性获取元素?

    p 你怎样才能做到相当于 document querySelectorAll data foo where 查询选择器全部 https developer mozilla org en DOM Document querySelectorA
  • 如何让 Rails 资产管道生成源地图?

    我想让 Rails 与编译后的 CoffeeScript 缩小的 JS 一起生成源映射 以便更好地记录错误 不过 网上似乎还没有关于如何执行此操作的全面文档 有人这样做过吗 我使用 Rails 3 2 和 Heroku Rails 支持缩小
  • 通过 JavaScript 获取页面/iframe 的编码

    我想通过 JavaScript 或浏览器中的其他一些 API 以编程方式确定页面的编码 我想要这些信息的原因是因为我试图对主要浏览器支持的字符编码进行模糊测试 显然仅仅因为我发送了适当的 内容类型 并不意味着浏览器会使用编码做正确的事情 欢
  • 如何在 jQuery/javascript 中获取边框宽度

    如何解析边框宽度 style border solid 1px black 在 jQuery javascript 中 elem css border width 不这样做 注意我需要解析 css 的宽度 因为元素可能是display no
  • jQuery:评估 ajax 响应中的脚本

    来自我的 web 应用程序的 XML 响应既有要添加到页面的 HTML 也有要运行的脚本 我正在尝试从我的网络应用程序发回 XML 例如
  • Firefox:按下鼠标按钮时鼠标悬停不起作用

    这是我想做的 https gfycat com ValidEmbarrassedHochstettersfrog https gfycat com ValidEmbarrassedHochstettersfrog 我想强调一些 td 对象在
  • 对同一域发出 get 请求,出现 CORS 错误

    在浏览器扩展中 这是我的 ajax 调用 var xhr new XMLHttpRequest xhr open GET window location href true xhr responseType arraybuffer xhr
  • 解决多个 jQuery 文件之间的冲突

    我的项目中有多个 jquery 文件 我正在使用jquery1 4 2使用facebox 但我也需要原型和scriptacolous脚本 我用过 jQuery noconflict 在我的代码中 但它不起作用 这是网址http mlep c
  • 更改 jQuery 中链接的标题

    我有一个 id 为 helpTopicAnchorId 的链接 我想在 jQuery 中更改其文本 我该怎么做呢 helpTopicAnchorId text newText P S the jQuery 文档 http docs jque
  • datatables.search 函数修改后的奇怪行为

    这个问题是后续问题这个问题 https stackoverflow com questions 54671211 overriding datatables js search behavior 我已经创建了这个 JSFiddle http
  • 通过 javascript 将 onsubmit 添加到表单

    您将如何仅通过 Javascript 将 OnSubmit 属性插入到表单中 我对 javascript 还很陌生 所以如果您能够提供详细的示例代码 那将是最有帮助的 情况是这样的 我通过 Chargify 支付平台 使用托管注册页面来处理
  • 让 hashchange 事件在所有浏览器(包括 IE7)中工作

    我有一些代码 由另一位开发人员编写 在 WordPress 内部进行 AJAX 页面加载 例如 没有页面重新加载 当您单击导航项时 AJAX 会刷新主要内容区域 我的问题是它在 IE7 中被破坏了 我不知道从哪里开始调试 最初的开场白是 v
  • .js.erb VS .js

    将 Rails 应用程序的 javascript 放入 js erb 文件而不只是将其放入 application js 文件有什么好处 我有一个企业创建按钮 因此我应该将代码放入 create js erb 文件中 还是使用以下方法将其放
  • 将变量从 PHP 传递到 JavaScript 的有效方法[重复]

    这个问题在这里已经有答案了 有时我必须将一些变量从 PHP 传递到 JS 脚本 现在我是这样做的 var js variable 但这非常丑陋 我无法在 js 文件中隐藏我的 JS 脚本 因为它必须由 PHP 解析 处理这个问题的最佳解决方

随机推荐