gpt4 book ai didi

javascript - 如何在 React JS 的组件中传递 props

转载 作者:行者123 更新时间:2023-12-01 01:44:44 24 4
gpt4 key购买 nike

您好,我有一个折叠包装器,默认情况下其显示状态为 true (我无法更改它)还有一个模态组件,在打开时需要显示 False。我认为将 defaultOpen 属性设置为“false”会将 Display 设置为 false。但这不起作用。我做错了什么?这是代码:

我的折叠包装:

import React, { Component } from "react";
import ChevronUp from "react-feather/dist/icons/chevron-up";
import ChevronDown from "react-feather/dist/icons/chevron-down";
import { Collapse } from "reactstrap";
import "./style.scss";

class CollapseWrapper extends Component {
constructor(props) {
super(props);

this.state = {
display:
props.defaultOpen === undefined ? true : props.defaultOpen,
title: this.props.title,
};
}

toggleContainer = () => {
this.setState(prevState => ({
display: !prevState.display,
}));
};

render() {
const { display, title } = this.state;
return (
<div>
<button type="button" onClick={this.toggleContainer}>
<div className="title-container">
{display ? (
<ChevronUp className="chevron" />
) : (
<ChevronDown className="chevron" />
)}

<h2>{title}</h2>
</div>
</button>
<Collapse isOpen={this.state.display}>
{this.props.children}
</Collapse>
</div>
);
}
}

export default CollapseWrapper;

我的模态:

import React from "react";
import { Modal } from "reactstrap";
import CollapseWrapper from "./CollapseWrapper";

class Mymodal extends Component {
constructor(props) {
super(props);
this.state = {};
}

render() {
const { isOpen } = this.props;
return (
<Modal size="xxl" isOpen={isOpen} toggle={this.close}>
<CollapseWrapper defaultOpen="false" title="More détails">
Some content...
</CollapseWrapper>
</Modal>
);
}
}

export default Mymodal;

最佳答案

您应该在大括号 {} 内传递 bool 值,而不是在字符串中传递。正确 defaultOpen={false}错误的 defaultOpen="false"

<CollapseWrapper defaultOpen={false} title="More détails">
Some content...
</CollapseWrapper>

关于javascript - 如何在 React JS 的组件中传递 props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52055816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com