简体   繁体   English

反应容器组件动作

[英]React container component action

I have a Connection component using an external library to get the network status. 我有一个使用外部库获取网络状态的Connection组件。

import Offline from '../lib/networkStatus';

export class Connection extends React.Component {
  componentDidMount() {
    this.props.onOffline && networkStatus.on('offline', this.props.onOffline);
  }

  render() {
    return this.props.children;
  }

  retry() {
    networkStatus.retry();
  }
}

Connection.propTypes = {
  onOffline: React.PropTypes.func
}

I use it as a container component, to allow children to have access to the onOffline event, to display a "Retry" link: 我将其用作容器组件,以允许孩子访问onOffline事件,并显示“重试”链接:

export class ConnectionRetry extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      offline: false
    }

    this.onOffline = this.onOffline.bind(this);
    this.onRetry= this.onRetry.bind(this);
  }

  onOffline() {
    this.setState({
      offline: true
    });
  }

  onRetry() {
    //TODO..
    // Call ConnectionStatus.retry
  }

  render() {
    return (
      <Connection onOffline={this.onOffline}>
        {!this.state.offline && 
          <a href="#" onClick={this.onRetry}>Retry</a>
        }
      </Connection>
    );
  }
}

I would like to call the Connection.retry method when we click on the Retry link. 当我们单击“重试”链接时,我想调用Connection.retry方法。
How is it possible? 这怎么可能? I presume the Connection component should be the only one to have access to the third part networkStatus library. 我假设Connection组件应该是唯一可以访问第三方networkStatus库的组件。

You can use refs , check Doc 您可以使用refs ,检查Doc

Add Refs to Component : ConnectionRetry 向组件添加引用: ConnectionRetry

render() {
    return (
      <Connection ref={(connection) => { this.connection = connection; }} onOffline={this.onOffline}>
        {!this.state.offline && 
          <a href="#" onClick={this.onRetry}>Retry</a>
        }
      </Connection>
    );
  }

Call method using refs 使用refs调用方法

 onRetry() {
     this.connection.retry()
 }

Full Source code for ConnectionRetry : ConnectionRetry完整源代码:

export class ConnectionRetry extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      offline: false
    }

    this.onOffline = this.onOffline.bind(this);
    this.onRetry= this.onRetry.bind(this);
  }

  onOffline() {
    this.setState({
      offline: true
    });
  }

  onRetry() {
     this.connection.retry()
  }

  render() {
    return (
      <Connection ref={(connection) => { this.connection = connection; }} onOffline={this.onOffline}>
        {!this.state.offline && 
          <a href="#" onClick={this.onRetry}>Retry</a>
        }
      </Connection>
    );
  }
}

Instead of adding a link inside your connection component what we can do is pass the this.retry as a prop. 除了在您的连接组件内添加链接之外,我们可以做的是传递this.retry作为prop。 so we will create a function to extract the logic from the connection component: 因此,我们将创建一个函数以从连接组件中提取逻辑:

export class ConnectionRetry extends React.Component {
  ...

  connectionProps() {
     if (this.state.offline) {

        return { retry: this.onRetry }
     }
  }
}   

render() {
     return (
      <Connection 
       onOffline={this.onOffline} 
       { ...this.connectionProps() } />
     );
}

and then in your ConnectionRetry component: 然后在ConnectionRetry组件中:

export class Connection extends React.Component {
 ...

render() {
   return(
     <div><a onlClick={this.props.retry}>retry</a></div>
   );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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