简体   繁体   English

在Redux容器中使用更高阶的组件

[英]Using higher order components with Redux containers

First, some context. 首先,一些背景。

I'm using Redux to manage authentication state of my app and have Auth as a Redux container (or smart component). 我正在使用Redux来管理我的应用程序的身份验证状态,并将Auth作为Redux容器(或智能组件)。

I've created a wrapper (a higher-order component) that takes Auth and returns it: 我创建了一个包装器(一个更高阶的组件),它接受Auth并返回它:

export default function AuthWrapper(WrappedComponent) {
  class Auth extends Component {
  ... <Auth stuff here> ...
  }
  return connect(mapStateToProps, mapDispatchToProps)(Auth);
}

It seems to me that in order to use the wrapper, I just need to invoke it with a component I want to have behind my auth. 在我看来,为了使用包装器,我只需要使用我想要在我的身份验证后面的组件来调用它。 For example, let's say I'm authenticating a component called UserPage with the wrapper, à la: 例如,假设我正在使用包装器来验证名为UserPage的组件,àla:

const AuthenticatedUserPage = AuthWappper(UserPage)

However, when I use the wrapper like this, React isn't happy with me. 但是,当我使用这样的包装器时,React对我不满意。 I get the following error: 我收到以下错误:

Warning: AuthenticatedApp(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

My best guess is that it doesn't like the connect -ified component that Redux will create when I return it from AuthWrapper ... which leads me to my question: 我最好的猜测是,当我从AuthWrapper返回时,它不喜欢Redux将创建的connect组件...这引出了我的问题:

Does React support higher-order components when those components create Redux containers? 当这些组件创建Redux容器时,React是否支持更高阶的组件? And if so, why would React be throwing this error? 如果是这样,为什么React会抛出这个错误?

Here's my two cents. 这是我的两分钱。 I think the error is occurring elsewhere. 我认为错误发生在其他地方。

According to this simplified version of the connect function in react-redux, the connect function is simply returning another react component. 根据react-redux中connect函数的这个简化版本,connect函数只是返回另一个反应组件。 So in your case, you're returning a component, wrapped inside another component , which is still valid. 因此,在您的情况下, 您将返回一个组件,包含在另一个组件中 ,该组件仍然有效。 A container is basically a component. 容器基本上是一个组件。

Read https://gist.github.com/gaearon/1d19088790e70ac32ea636c025ba424e for a better understanding of the connect function. 请阅读https://gist.github.com/gaearon/1d19088790e70ac32ea636c025ba424e,以便更好地了解连接功能。

I also tried the following in my own application and it worked. 我也在我自己的应用程序中尝试了以下内容并且它有效。

import Layout from '../components/Layout'
//Do some other imports and stuff
function wrapper(Layout) {
  return connect(null, mapDispatchToProps)(Layout);
}
export default wrapper()

Like the error states, you might just simply be returning an invalid component somewhere in your app. 与错误状态一样,您可能只是在应用程序中的某个位置返回无效组件。 Your app might be throwing the error because you're not wrapping a return call in parentheses on your render method. 您的应用可能会抛出错误,因为您没有在渲染方法的括号中包含返回调用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM