0

I created a project with create-react-app:

yarn create react-app example-react-ts --template typescript

Then created a simple component:

import * as React from 'react'

interface ExampleProps {message: string}

class Example extends React.Component<ExampleProps> {
    render() {
        console.log("render")
        return (<div><p>{this.props.message}</p></div>)
    }
}

export default Example

Firefox prints "render" once.

Chrome prints "render" twice.


If I replace it with a functional component:

import * as React from 'react'
interface ExampleProps {message: string}

export const Example = (props: ExampleProps) => {
    console.log("render")
    return (
        <div><p>{props.message}</p></div>
    );
}

export default Example

Both Firefox and Chrome print "render" once.

Why?

cahen
  • 15,807
  • 13
  • 47
  • 78

1 Answers1

1

The problem is the Strict Mode used by default when you create the project with create-react-app:

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

Removing it solved the problem.

More details here.

cahen
  • 15,807
  • 13
  • 47
  • 78