reactjs - useEffect 在高阶组件中调用

标签 reactjs redux higher-order-components

更新后,React 不再使用高阶组件 (HOC) 中的 useEffect Hook 编译代码。我有一个 HOC,它连接到 Redux 存储并在需要时分派(dispatch)一个操作来获取数据。

import React, {useEffect} from 'react'
import PropTypes from 'prop-types' 
import { connect } from 'react-redux'

import SpinCenter from '../components/SpinCenter'
import { fetchObject } from '../actions/objects'
import { fetchIfNeeded } from '../utils'


const withObject = ({element}) => WrappedComponent => ({id, ...rest}) => {
  const hoc = ({status, object, fetchObject}) => {
    useEffect(() => {
      fetchIfNeeded(
        status,
        ()=>fetchObject(element, id),
      )
    })

    // Initial loading and error
    if (status === undefined || object === undefined) return <SpinCenter/>
    if (status.error) return <>error loading: {status.error.message}</>

    // Pass through the id for immediate access
    return <WrappedComponent {...{object, status, id}} {...rest} />
  }

  hoc.propTypes = {
    object: PropTypes.object,
    status: PropTypes.object,
    fetchObject: PropTypes.func.isRequired,
  }

  const mapStateToProps = state => ({
    object: state.data[element][id],
    status: state.objects[element][id]
  })

  const mapDispatchToProps = {
    fetchObject
  }

  const WithConnect = connect(mapStateToProps, mapDispatchToProps)(hoc)
  return <WithConnect/>
}

export default withObject

我希望这是有道理的。我想最好的方法是以某种方式将 useEffect 放入功能组件中,但是这一切应该发生的地方变得有点复杂。谁能帮助我理解这一点?

这是我收到的错误。

React Hook "useEffect" is called in function "hoc" which is neither a React function component or a custom React Hook function

最佳答案

您的组件名称需要以大写字符开头,这就是您遇到此问题的原因。您还可以优化连接和临时代码以返回临时实例一次,而不是一次又一次地执行

const withObject = ({element}) => WrappedComponent => {
  const Hoc = ({id, status, object, fetchObject,...rest}) => {
    useEffect(() => {
      fetchIfNeeded(
        status,
        ()=>fetchObject(element, id),
      )
    })

    // Initial loading and error
    if (status === undefined || object === undefined) return <SpinCenter/>
    if (status.error) return <>error loading: {status.error.message}</>

    // Pass through the id for immediate access
    return <WrappedComponent {...{object, status, id}} {...rest} />
  }

  Hoc.propTypes = {
    object: PropTypes.object,
    status: PropTypes.object,
    fetchObject: PropTypes.func.isRequired,
  }

  const mapStateToProps = state => ({
    object: state.data[element][id],
    status: state.objects[element][id]
  })

  const mapDispatchToProps = {
    fetchObject
  }

  return connect(mapStateToProps, mapDispatchToProps)(Hoc)
}

export default withObject

关于reactjs - useEffect 在高阶组件中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58832843/

相关文章:

javascript - 当我拖动一个元素时,react-sortable-hoc 列表消失

reactjs - 将 Material-ui 自定义主题与 redux 结合使用

javascript - 从 React 类修改 React 元素

css - ReactCSSTransitionGroup 的麻烦 - 试图获得平滑的滑入/滑出动画

node.js - 使用 Azure AD、ReactJS 和 NodeJS 对用户进行身份验证并使用 Graph API

javascript - 如何更新所有打开的选项卡中的本地存储

javascript - ReactJS + Redux : How to use Material-UI's RaisedButton as &lt;input/>?

reactjs - Jest/ enzyme 单元测试 : How to pass store to shallow component that uses redux 4 and react-redux 6 connect function

javascript - 如何使用 JavaScript 选择 GatsbyJS 中的元素

angular - 解决 Action 创建者中的异步函数