简体   繁体   中英

How to pass props in my components inReactJS

Hello I have a collapse wrapper which has its Display state as true by default (I can't change that) And a modal component which needs the display False when it opens. I thought that setting the defaultOpen props as "false" would set the Display to false. But it doesn' work. What do I do wrong ? Here is the code :

My Collapse wrapper :

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;

My modal :

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;

use arrow function in onClick event on your button

replace

onClick={this.toggleContainer}

to

onClick={() => this.toggleContainer()}

You should pass boolean value inside the curly braces {} not in string. Correct defaultOpen={false} wrong defaultOpen="false"

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

I think, you can use component life cycle method componentWillReceiveProps(nextProps) to solve this issue. Set your display state again when componentWillReceiveProps.

Solution:

componentWillReceiveProps(nextProps) {
   if (nextProps.defaultOpen !== this.state.display) {
     this.setState({ ...this.state, display: nextProps.defaultOpen });
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM