Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改反应组件';单击另一个组件时的状态_Javascript_Reactjs - Fatal编程技术网

Javascript 更改反应组件';单击另一个组件时的状态

Javascript 更改反应组件';单击另一个组件时的状态,javascript,reactjs,Javascript,Reactjs,我正试图根据某个组件的状态显示/隐藏该组件,并希望在第三个组件中单击后对其进行更改 //导航栏 export class NavigationBar extends React.Component { constructor(props) { super(props); this.state = { showNotification: false, } } handleNotification = () => this.setState({

我正试图根据某个组件的状态显示/隐藏该组件,并希望在第三个组件中单击后对其进行更改

//导航栏

export class NavigationBar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showNotification: false,
    }
  }

  handleNotification = () => this.setState({
    showNotification: !this.state.showNotification,
  });

  { this.state.showNotification ? <Outside><Notifications /></Outside> : null}
导出类导航栏扩展React.Component{
建造师(道具){
超级(道具);
此.state={
showNotification:false,
}
}
handleNotification=()=>this.setState({
showNotification:!this.state.showNotification,
});
{this.state.showNotification?:null}
//外部组件,负责检测是否在其外部发生单击

export default class Outside extends Component {
  constructor(props) {
    super(props);
    this.setWrapperRef = this.setWrapperRef.bind(this);
    this.handleClickOutside = this.handleClickOutside.bind(this);
  }
  componentDidMount() {
     document.addEventListener('mousedown', this.handleClickOutside)
  }

  setWrapperRef(node) {
    this.wrapperRef = node;
  }

  handleClickOutside(event) {
    if(this.wrapperRef && !this.wrapperRef.contains(event.target)) {
      console.log("clicked outside notifications");
      this.setState({
        showNotification: false
      })
    }
  }

  render() {
    return (
      <div ref={this.setWrapperRef}>
         {this.props.children}
      </div>
    )
  }
}

Outside.propTypes = {
  children: PropTypes.element.isRequired
}
导出扩展组件外部的默认类{
建造师(道具){
超级(道具);
this.setWrapperRef=this.setWrapperRef.bind(this);
this.handleClickOutside=this.handleClickOutside.bind(this);
}
componentDidMount(){
document.addEventListener('mousedown',this.handleClickOutside)
}
setWrapperRef(节点){
this.wrapperRef=节点;
}
handleClickOutside(事件){
if(this.wrapperRef&&!this.wrapperRef.contains(event.target)){
日志(“单击外部通知”);
这是我的国家({
showNotification:错误
})
}
}
render(){
返回(
{this.props.children}
)
}
}
Outside.propTypes={
子项:PropTypes.element.isRequired
}

我的疑问是,如何根据在
外部组件内部检测到的事件更改导航栏中的状态?

在父级中,您需要向下到事件处理程序外部:

<Outside toggleNofitication={this.handleNotification}><Notifications /></Outside>

在父级中,您需要向下到事件处理程序外部:

<Outside toggleNofitication={this.handleNotification}><Notifications /></Outside>

你有没有一个例子?它抛出了一个错误“this.props.toggleNotification不是外部的函数”哦,对不起,我的错误。将
toggleNotification
传递到
Outside
而不是
通知
。查看我更新的ASWER你有例子吗?它抛出了一个错误“this.props.toggleNotification不是外部的功能”哦,对不起,我的错误。将
toggleNotification
传递到
外部
而不是
通知
。请参阅我的更新答案