3

I'm using a HOC to render a React Component using a HOC and receiving the above error.

The component looks like this

import React, { Component } from 'react'
import { hot } from 'react-hot-loader'
import withDashboardForm from './../form/withDashboardForm'

class LoginForm extends Component {
  render() {
    return (
      <form id="login-form" className="dashboard-form" method="post" onSubmit={(e) => this.props.handleSubmit(e)} encType="multipart/form-data">
        .. form definition goes here
      </form>
    )
  }
}

export default hot(module)(withDashboardForm(LoginForm))

This is the HOC:

    const withDashboardForm = (WrappedComponent) => {
      return class ComponentWithDashboardForm extends Component {

        constructor(props) {
          super(props)
          ...
        }

        componentDidMount() {
          ...
        }

        handleChange(e) {
          ..
        }

        handleSubmit(e) {
          ...
        }

        render() {
          return (
            <WrappedComponent
              {...this.props}
              {...this.state}
              handleChange={this.handleChange}
              handleSubmit={this.handleSubmit}

            />
          )
        }

      }
    }

    export default hot(module)(withDashboardForm)

Finally the render method of React Router is used to render the component like so

<Route exact path="/teacher/login" render={(props) => <LoginForm {...props} handler={new LoginFormHandler()}/>}/>

When I run npm start everything works as expected (it runs "NODE_ENV=production node ./dist/server.generated.js").

But when I start the server using pm2 which runs "node /home/ubuntu/node/ris/dist/server.generated.js" I see the TypeError mentioned in the title above.

2 Answers 2

2

I ran through the same problem but the above answer didn't fix my problem. React has announced a new feature called "React fast refresh" which can replace "React hot loader", and it is officially supported by react. You can use below instructions to add "fast refresh":

  1. yarn add -D react-refresh
  2. yarn add -D @pmmmwh/react-refresh-webpack-plugin
  3. Add react-refresh/babel to babel config file.
  4. Add react-refresh-webpack-plugin plugin to the webpack configuration file:
// webpack.config.js
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
module.exports = {
  ...
  plugins: [
    new ReactRefreshWebpackPlugin(),
  ],
};
  1. Finally, remove 'react-hot-loader' from your project.
Sign up to request clarification or add additional context in comments.

2 Comments

This worked like a charm! Thanks for the proper breakdown of steps to follow.
I am happy that it helped ;-)
0

This error probably comes from export default hot(module)(withDashboardForm). This returns a class, but you are using it as a function in export default hot(module)(withDashboardForm(LoginForm)).

There are two possible solutions:

  1. don't use hot(module) on the HOC
  2. instanciate the class with new, like export default hot(module)(new withDashboardForm(LoginForm))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.