【问题标题】:Extend from React.Component class and connect to redux-store从 React.Component 类扩展并连接到 redux-store
【发布时间】:2017-05-16 19:51:55
【问题描述】:

我有一个扩展 react.component 的类。另一个类从这个类扩展并连接到一个 redux-store。

import React from 'react'
class A extends React.Component{
    constructor(props){super(props); ...}
    ...
}

import React from 'react'
import {connect} from 'react-redux'
@connect(store=>{...})
class B extends A{
    constructor(props){super(props); ...}
    ...
}

class C extend A{....}

不幸的是,这不起作用。难道不显式扩展 React.Component 就可以将类连接到商店吗?

错误:

Uncaught TypeError: Super expression must either be null or a function, not undefined

【问题讨论】:

  • 似乎应该可以工作。您能否提供更多关于正在发生或未发生的事情的信息?
  • 你应该避免在组件之间做继承,最好只从 React.Component 继承
  • 例如:我有几个列表组件,并且都共享相同的行为(处理程序等)。为什么我不应该将此函数导出到我扩展并仅添加渲染函数的组件中。 (我找不到合适的设计模式)
  • 问题不正常,请看我的回答

标签: reactjs redux


【解决方案1】:

我通过 index.js 导入了 a 类。这不起作用,导入文件本身就起作用了。

【讨论】:

    【解决方案2】:

    正如Vincent Taing 所说,您尝试做的事情不是一个好习惯。而是使用 HOC(高阶组件)来处理您的所有逻辑(处理程序等),将此组件与您的商店连接,并使用您的处理程序返回您的 UI 组件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-25
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2017-12-17
      • 2019-09-18
      相关资源
      最近更新 更多