简体   繁体   English

带HOC的Redux Connect-TypeError:无法设置未定义的属性“ props”

[英]Redux Connect w/ HOC - TypeError: Cannot set property 'props' of undefined

I'm building a quick authentication higher order component in Next.js and am getting some problems with the following code: 我正在Next.js中构建一个快速身份验证高阶组件,并且以下代码出现了一些问题:

import SignIn from "../components/sign-in";
import { connect } from "react-redux";
import { useRouter } from "next/router";

const AuthenticationCheck = WrappedComponent => {
  const { isAuthenticated, ...rest } = props;
  const router = useRouter();
  const protectedPages = ["/colours", "/components"];
  const pageProtected = protectedPages.includes(router.pathname);

  return !isAuthenticated && pageProtected ? (
    <SignIn />
  ) : (
    <WrappedComponent {...rest} />
  );
};

function mapStateToProps(state) {
  return {
    isAuthenticated: state.auth.isAuthenticated
  };
}

export default connect(mapStateToProps)(AuthenticationCheck);

If I change the code to remove redux & connect, it looks like this, and works perfectly. 如果我更改代码以删除redux&connect,它看起来像这样,并且运行良好。

const AuthenticationCheck = WrappedComponent => {
  const { ...rest } = props;
  const router = useRouter();
  const protectedPages = ["/colours", "/components"];
  const pageProtected = protectedPages.includes(router.pathname);

  return pageProtected ? <SignIn /> : <WrappedComponent {...rest} />;
};

export default AuthenticationCheck;

I've been reading every SO, redux documentation etc for the last couple of hours, and I can't really find anything that matches what I'm doing, although I can't believe it's an uncommon use case. 在过去的几个小时中,我一直在阅读每个SO,redux文档等内容,尽管我不敢相信这是一个不常见的用例,但我找不到真正与我正在做的事情匹配的东西。

Am I missing something obvious? 我是否缺少明显的东西?

Solution: (Thankyou Dima for your help!) 解决方案:(谢谢迪玛的帮助!)

So the final code that ended up working is: 因此,最终工作的最终代码是:

import SignIn from "../components/sign-in";
import { connect } from "react-redux";
import { useRouter } from "next/router";
import { compose } from "redux";

const AuthenticationCheck = WrappedComponent => {
  const authenticationCheck = props => {
    const { isAuthenticated, ...rest } = props;
    const router = useRouter();
    const protectedPages = ["/colours", "/components"];
    const pageProtected = protectedPages.includes(router.pathname);

    return !isAuthenticated && pageProtected ? (
      <SignIn />
    ) : (
      <WrappedComponent {...rest} />
    );
  };
  return authenticationCheck;
};

function mapStateToProps(state) {
  return {
    isAuthenticated: state.auth.isAuthenticated
  };
}

export default compose(connect(mapStateToProps), AuthenticationCheck);

This works perfectly! 这样完美! 🙂 🙂

connect expects to get React component as a last argument, but you are sending HOC instead. connect希望将React组件作为最后一个参数,但是您正在发送HOC。 You need to put connect and wrapper inside compose function. 您需要将connect和包装器置于compose函数内。 See below 见下文

 import React from 'react' import {compose} from 'redux' import {connect} from 'react-redux' import {doSomething} from './actions' const wrapComponent = Component => { const WrappedComponent = props => { return ( <Component {...props} /> ) } return WrappedComponent } const mapStateToProps = state => { return { prop: state.prop, } } export default compose( connect(mapStateToProps, {doSomething}), wrapComponent ) 

And the useit like this. 和这样的用处。

 import React from 'react' import withWrapper from 'your/path' const Component = props => 'Component' export default withWrapper(Component) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法设置undefined的属性&#39;props&#39; - TypeError: Cannot set property 'props' of undefined React - TypeError:无法设置未定义的属性'props' - React - TypeError: Cannot set property 'props' of undefined React Redux - TypeError:无法读取未定义的属性“道具” - React Redux - TypeError: Cannot read property 'props' of undefined React Redux - Uncaught(in promise)TypeError:无法读取未定义的属性&#39;props&#39; - React Redux - Uncaught (in promise) TypeError: Cannot read property 'props' of undefined 未捕获的TypeError:无法读取react-redux中未定义的属性&#39;props&#39; - Uncaught TypeError: Cannot read property 'props' of undefined in react-redux React/Redux - TypeError:无法读取未定义的属性“道具” - React/Redux - TypeError: Cannot read property 'props' of undefined React-redux:无法使用connect获取作为道具的商店,无法读取未定义错误的属性“ props” - React-redux: cannot fetch the store as props using connect, Cannot read property 'props' of undefined error React-router:TypeError:无法设置未定义的属性“props” - React-router: TypeError: Cannot set property 'props' of undefined React DnD 与 HOC 可拖动元素抛出无法设置未定义错误的属性“道具” - React DnD with HOC draggable elements throw Cannot set property 'props' of undefined error React Redux:无法读取未定义的属性“道具” - React Redux: Cannot read property 'props' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM