美文网首页让前端飞es6前端开发那些事
react如何将组件内部的方法暴露给外部

react如何将组件内部的方法暴露给外部

作者: mytac | 来源:发表于2018-06-26 11:45 被阅读41次

最近在项目中遇到一个问题,就是需要在类的外部调用操作类内部的方法。

举个例子,我有一个Toast组件,在外部需要调用它的show方法来控制他的显隐状态。
之前我的写法是写一个静态类方法,然后在constructor中去修改它的作用域,代码如下:

// @flow
import React from 'react';
import './style.less';

type Props={
    time:number,
    text:string,
}

export default class Toast extends React.Component {
    static show({ text }) {
        const _self = this;
        this.setState({
            isShow: true,
            text,
        }, () => {
            _self.timer = setTimeout(() => {
                this.setState({
                    isShow: false,
                });
            }, this.props.time);
        });
    }

    constructor(props:Props) {
        super(props);
        this.state = {
            isShow: false,
            text: '',
        };
        Toast.show = Toast.show.bind(this);
    }

    componentWillUnmount() {
        this.setState({
            isShow: false,
        });
        clearInterval(this.timer);
    }

    render() {
        const { isShow, text } = this.state;
        return (
            <div className="toast-wrapper">
                {isShow && <div className="toast">{text}</div>}
            </div>
        );
    }
}

后来发现bug频出,只有在重新刷新的时候show方法中调用this.setState()才有效,而在多个页面使用这个组件会出现很多问题,都是由于作用域绑定错误的原因。

解决方法

最后审视了下代码,应该是将静态的show方法指向内部的show方法,而不是把静态的show方法的上下文绑定到这个类上。

调整代码如下:

// ...
 constructor(props:Props) {
        super(props);
        this.state = {
            isShow: false,
            text: '',
        };
        Toast.show = this.show.bind(this); // 最重要的一步!!
    }

    show({ text }) {
        const _self = this;
        this.setState({
            isShow: true,
            text,
        }, () => {
            _self.timer = setTimeout(() => {
                this.setState({
                    isShow: false,
                });
            }, this.props.time);
        });
    }
// ....

还是因为自己对bind理解不深刻的原因,看下mdn--bind

相关文章

  • react如何将组件内部的方法暴露给外部

    最近在项目中遇到一个问题,就是需要在类的外部调用操作类内部的方法。 举个例子,我有一个Toast组件,在外部需要调...

  • kubernetes各种port关系

    容易混淆的概念: 1、NodePort和port 前者是将服务暴露给外部用户使用并在node上、后者则是为内部组件...

  • kubernetes那些事儿

    容易混淆的概念: 1、NodePort和port 前者是将服务暴露给外部用户使用并在node上、后者则是为内部组件...

  • android常见漏洞

    一、组件本地拒绝服务漏洞: 漏洞描述: 组件暴露给外部后,在外部可以通过Intent启调app中的组件,如果组件中...

  • react重点

    React组件的生命周期(React16.4版本) 1.constructor方法 用于组件内部的state初始化...

  • react-父子组件间通信

    React-父子组件间通信 父组件传 值 给子组件, 传 方法 给子组件 子组件接收 值 ,触发父组件的 方法

  • react的state和props

    state状态和props属性是react非常重要的两个方面,状态控制组件内部的变化,属性获取外部传递给内部的数据...

  • 组件ref的使用

    自己封装的组件,想使用ref,调用内部的方法1.使用React.forwardRef,包裹组件。 2.接收ref属...

  • React 组件内部,方法的顺序

    生命周期方法按照时间先后顺序依次为:getDefaultPropsgetInitialStatecomponent...

  • 权限的坑

    getExternalFilesDir()方法获取自己的存储地址时候内部文件外部无权访问,如果想暴露可以使用 来获...

网友评论

  • BubbleM:为什么我这么操作提示this找不到呢 暴露出去的方法操作了当前组件的state
    ebfc7d0362e4:为啥搞这么麻烦不直接用 ref ?
    mytac:还不行的话贴一下代码
    mytac:尝试在constructor这样写:Toast.show =this.show= this.show.bind(this);

本文标题:react如何将组件内部的方法暴露给外部

本文链接:https://www.haomeiwen.com/subject/ahbpyftx.html