使用Alt.js的connectToStores时如何避免丢失React组件方法?

时间:2016-01-07 05:09:15

标签: javascript reactjs flux reactjs-flux alt.js

我使用Alt.js的connectToStores来访问组件内的存储状态,但是当我尝试从另一个组件(使用ref)调用该组件的方法时,该方法似乎由于包装器组件而丢失(给出未定义的方法)。

comp_a.js

class CompA extends React.Component {

    ...

    static getStores() { ... }
    static getPropsFromStores() { ... }

    ...

    method() { 
    }

    ...
}

export default connectToStores(CompA);

comp_b.js

class CompB extends React.Component {

    ...

    test() {
        // Causes error because "method()" is lost during connectToStores export
        // It seems that connectToStores doesn't inherit the base component, instead it wraps the component with another component.
        this.refs.other.method(); 
    }

    ...

    render() { 
        return (
            <CompA ref="other" />
        );
    }

    ...

}
  1. 有没有办法仍然可以调用方法()

  2. 有没有办法访问包装组件?

0 个答案:

没有答案