reactjs 从其他组件切换字段集

y0u0uwnf  于 2023-01-17  发布在  React
关注(0)|答案(1)|浏览(128)

如何使PrimeReact FieldSet可从React上的其他(父)组件控制?
我有一个布尔对象toggled,我想把它传递给子组件。
toggled = true时,应打开字段集。
toggled = false时,应关闭字段集。

import React from "react";
import { Fieldset } from "primereact/fieldset";

const FieldsetDemo = () => {
  return (
    <div>
      <div className="card">
        <h5>Toggleable</h5>
        <Fieldset legend="Header" toggleable>
          <p>
            Lorem ipsum dolor sit amet.
          </p>
        </Fieldset>
      </div>
    </div>
  );
};
klsxnrf1

klsxnrf11#

很简单。工作代码沙箱:https://codesandbox.io/s/quizzical-snow-so91tv?file=/src/demo/FieldsetDemo.js

const FieldsetDemo = (props) => {
  return (
    <div>
      <div className="card">
        <h5>Toggleable</h5>
        <Fieldset legend="Header" toggleable collapsed={props.collapsed}>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
            eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
            ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
            aliquip ex ea commodo consequat. Duis aute irure dolor in
            reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
            pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
            culpa qui officia deserunt mollit anim id est laborum.
          </p>
        </Fieldset>
      </div>
    </div>
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<FieldsetDemo collapsed={true} />, rootElement);

相关问题