javascript - DisplayHide a child component in React - Stack Overflow

I have the following code in a React render function:<div><InnerBox><div>Box 1<di

I have the following code in a React render function:

<div>
    <InnerBox>
        <div>Box 1</div>
        <HiddenBox />
    </InnerBox>

    <InnerBox>
        <div>Box 2</div>
        <HiddenBox />
    </InnerBox>

    <InnerBox>
        <div>Box 3</div>
        <HiddenBox />
    </InnerBox>
</div>

I would like each of the <HiddenBox /> elements to be hidden until the <InnerBox> element is clicked on. Any other child elements of <InnerBox> should be displayed at all times.

I currently have the following function in my InnerBox class:

handleClick(){
    this.setState({
        button_state: "clicked"
    })
}

And my render function of the InnerBox class looks something like this:

return (
    <div onClick={this.handleClick.bind(this)}>{this.props.children}</div>
)

However, I am a bit confused as to where exactly the code would go to toggle the display of the HiddenBox element for the specific InnerBox that was clicked on.

How do I go about doing this?

I have the following code in a React render function:

<div>
    <InnerBox>
        <div>Box 1</div>
        <HiddenBox />
    </InnerBox>

    <InnerBox>
        <div>Box 2</div>
        <HiddenBox />
    </InnerBox>

    <InnerBox>
        <div>Box 3</div>
        <HiddenBox />
    </InnerBox>
</div>

I would like each of the <HiddenBox /> elements to be hidden until the <InnerBox> element is clicked on. Any other child elements of <InnerBox> should be displayed at all times.

I currently have the following function in my InnerBox class:

handleClick(){
    this.setState({
        button_state: "clicked"
    })
}

And my render function of the InnerBox class looks something like this:

return (
    <div onClick={this.handleClick.bind(this)}>{this.props.children}</div>
)

However, I am a bit confused as to where exactly the code would go to toggle the display of the HiddenBox element for the specific InnerBox that was clicked on.

How do I go about doing this?

Share Improve this question edited Oct 25, 2018 at 23:20 kojow7 asked Oct 25, 2018 at 23:13 kojow7kojow7 11.5k21 gold badges93 silver badges147 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

To toggle hide and show.

export default class InnerBox extends React.Component {
  state = {
    buttonClicked: false
  };
  handleClick = () => {
    this.setState({
      buttonClicked: !this.state.buttonClicked
    });
  };
  render() {
    return (
      <div onClick={this.handleClick}>
        {this.props.children}
        {this.state.buttonClicked && <HiddenBox />}
      </div>
    );
  }
}

If I understand correctly, then you could resolve this by updating the rendering of <InnerBox/> in this way:

<div>
    <InnerBox>
        <div>Box 1</div>
    </InnerBox>

    <InnerBox>
        <div>Box 2</div>
    </InnerBox>

    <InnerBox>
        <div>Box 3</div>
    </InnerBox>
</div>

You could then update the render() function of <InnerBox/> like so:

render() {

    const { button_state } = this.state;
    const isButtonStateClicked = button_state === 'clicked';

    return (<div onClick={this.handleClick.bind(this)}>
    { this.props.children }
    { isButtonStateClicked && <HiddenBox /> }
    </div>)
}

Finally, you'd update handdleClick() in this way to achieve to hide/show toggle behaviour:

handleClick(){
    this.setState({
        button_state: this.state.button_state === 'clicked' : '' : 'clicked'
    })
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744717523a4589708.html

相关推荐

  • javascript - DisplayHide a child component in React - Stack Overflow

    I have the following code in a React render function:<div><InnerBox><div>Box 1<di

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信