【问题标题】:call class function from export const react native从 export const 调用类函数 react native
【发布时间】:2019-03-18 06:51:38
【问题描述】:

我想从 webViewEventHandlers.js 中的 export const 类调用 ComposeBox.js 中的 handleSend 函数

ComposeBox.js

class ComposeBox extends PureComponent<Props, State> {

handleSend = () => {
    const { dispatch } = this.props;
    const { message } = this.state;
    dispatch(addToOutbox(this.getDestinationNarrow(), message));

    this.setMessageInputValue('');
  };

export default connect((state: GlobalState, props) => ({
  auth: getAuth(state),
}))(ComposeBox);

webViewEventHandlers.js

import ComposeBox from '../compose/ComposeBox';

export const handleMessageListEvent = () => {

case 'wizrep':
      ComposeBox.handleSend();
      break;

我收到以下错误

【问题讨论】:

  • this的可能重复
  • 谢谢兄弟,但它只适用于静态函数。
  • 我有点明白你想做什么。但请记住一件事。如果你想调用一个类的函数,你需要一个它的实例。例如class Foo{function method(){//do smth}} class Boo { function meth(){ var a = new Foo(); a.method();}}
  • 我试图实现你的例子case 'wizrep': var test = new ComposeBox(); test.handleSend(); break;Cannot read property 'store' of undefined
  • yes redux 我猜会导致这个错误。我认为您最好找到解决问题的新方法。

标签: reactjs react-native


【解决方案1】:

React 手册指出:All React components must act like pure functions with respect to their props.

这意味着尝试创建组件的实例并将其视为对象将给您带来非常困难的时间。

根据您发送的代码的 sn-p,我可以想象一些可能的解决方法:

  • 从 ComposeBox 内部调用 handleMessageListEvent,将 handleSend 函数作为参数传递。
  • 将消息设置为 Redux 状态的一部分而不是组件,并从 webViewEventHandlers 调度操作。
  • 使 handleMessageListEvent 成为 ComposeBox 组件的一部分。

【讨论】:

    猜你喜欢
    • 2016-12-26
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    相关资源
    最近更新 更多