Drew2
Drew2

Reputation: 401

React-redux connect HOC as a class decorator, @connect

I have a component wrapped using the ES+ proposed decorator syntax form of react-redux's connect HOC:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import RegisterForm from './register-form';
import { registerUser } from '../store/api/Auth/actions';

@connect(null, { registerUser: registerUser })
export default class RegisterPage extends Component {
  handleSubmit = values =>
    this.props.registerUser(values);

  render() {
    return (
      <div>
        <h1>Register Here</h1>
        <RegisterForm onSubmit={this.handleSubmit} />
      </div>
    );
  }
}

I'm receiving an error:

Uncaught Error: You must pass a component to the function returned by connect. Instead received {"kind":"class","elements":[{"kind":"field","key":"handleSubmit","placement":"own","descriptor":{"configurable":true,"writable":true,"enumerable":false}},{"kind":"method","key":"render","placement":"prototype","descriptor":{"writable":true,"configurable":true,"enumerable":false}}]}

I'm using Webpack 4 with babel-loader. My .babelrc file is:

{
  "presets": [
      "@babel/preset-env",
      "@babel/preset-react"
    ],
    "plugins": [
      "@babel/plugin-proposal-class-properties",
      ["@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true }]
    ]
}

It seems to me that the target object of the connect function is the class, but the connect function is receiving the property descriptors of that class. I understand that decorators are discouraged by the React and Redux teams. This is a related SO question: React-Redux @connect syntax error

How can I get the @connect decorator to work?

Upvotes: 3

Views: 1060

Answers (1)

uni-zheng
uni-zheng

Reputation: 11

Make sure you inject decorator plugin before plugin class properties

{
  "plugins": [
    "@babel/plugin-proposal-decorators",
    "@babel/plugin-proposal-class-properties"
  ]
}

babel-plugin-proposal-decorators

Upvotes: 1

Related Questions