如何在React-redux中单击显示/隐藏组件?

kua*_*tro 13 reactjs redux

我有一个组件:

class CommentBox extends Component {
    render() {
        return (
            <div>
                <p>Some comment</p>
                <a>Post a reply to this comment</a>
            </div>
            <ReplyForm />
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要在初始加载时隐藏这个ReplyForm.如何通过单击标签来触发它的状态?

t1m*_*m0n 20

你应该在状态中添加一些标志CommentBox.如果此标志的值是false何时不显示ReaplyFrom,反之亦然.下面是代码和工作示例http://codepen.io/anon/pen/KzrzQZ

class ReplyForm extends React.Component {
  constructor() {
    super()
  }
  render(){
    return(
      <div>I'm ReplyForm</div>
    )
  }
}

class CommentBox extends React.Component {
  constructor() {
    super();
    this.state = {
      showReply: false
    }
  }
  onClick(e){
    e.preventDefault();
    this.setState({showReply: !this.state.showReply})
  }
  render() {
    return (
      <div>
        <p>Some comment</p>
         <a onClick={this.onClick.bind(this)} href='#'>Post a reply to this comment</a>
        {this.state.showReply && < ReplyForm / >}
      </div>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)