如何在类组件中使用分派或如何反映我的HOC

我有下面的HOC组件,该组件工作正常,但在调度上给我一个错误。我不能在类组件中使用它,但如何解决呢?

const withAuthentication = <Props extends object>(
  Component: React.ComponentType<Props>
) => {
  class WithAuthentication extends React.Component<Props & FirebaseInterface> {
    render(): React.ReactNode {
      const { firebase,...props } = this.props
      const dispatch = useDispatch()
      const [authenticated,setauthenticated] = useState(false)
      const { userId,loggedIn } = useSelector(
        (state: Record<string,ReduxProvider>) => state.user
      )

      useEffect(() => {
        const listener = firebase.auth.onAuthStateChanged(authUser => {
          if (authUser) {
            console.log(authUser)
            if (!loggedIn) {
              firebase.user(userId).once('value',snapshot => {
                dispatch(
              )
              })
            }

            setauthenticated(true)
          } else {
            dispatch(
              addUser({

              })
            )

            setauthenticated(false)
          }
        })

        return (): void => {
          listener()
        }
      },[setauthenticated,firebase,dispatch,loggedIn,userId])
    }
  }

  return withFirebase(WithAuthentication)
}

export default withAuthentication

任何帮助将不胜感激!

vera0101 回答:如何在类组件中使用分派或如何反映我的HOC

您不能在类组件中使用useDispatch。

您应该使用import。

import {connect} from 'react-redux'
import {compose} from 'redux'

...
const {dispatch} = this.props
...
return compose(withFirebase,connect(state => ({}),dispatch => ({dispatch})) )(WithAuthentication)
...

...
本文链接:https://www.f2er.com/3134256.html

大家都在问