[英]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.