Search code examples
reactjsnpmcreate-react-appreact-hooks

Create react app with npx that uses classes and not hooks


I want to use npx create-react-app . to generate a new react app but I want the version to be changed so that it generates with class structures and not the new hook structure.

I've tried changing the version of react and React-DOM in the package.json and running npm i but it doesn't overwrite the old app.


Solution

  • If I remember correctly, the boilerplate App.js created doesn't use hooks at all, but it does use functional components. Just replace it to something of the likes:

    Functional Component Version:

    import React from 'react';
    
    function App() {
      return (
        < ...this will go inside the render() method... >
      );
    }
    

    Class Component:

    import React, { Component } from 'react';
    
    class App extends Component {
      render() {
        return (< ... >)
      }
    }
    

    Don't forget the { Component } import, or alternatively use extends React.Component.