This React.Component class constructor seems to be run 2 times for one instance, in a weird way.
Apparently:
- the counter is incremented
0->1without a console.log output - the counter is incremented
1->2with a console.log output
Is this true ? Why does the console show 2 and not 1 ?
import React from 'react';
var instanceCount = 0;
console.log('root:', instanceCount);
class App extends React.Component {
constructor(props) {
super(props);
console.log('before change:', instanceCount);
instanceCount++;
console.log('after change:', instanceCount);
}
render() {
return (
<>test</>
);
}
}
export default App;
// console output:
// root: 0
// before change: 1
// after change: 2
In contrast, a "normal" javascript class behaves as expected:
var instanceCount = 0;
class SomeClass {
constructor(props) {
console.log('before change:', instanceCount);
instanceCount++;
console.log('after change:', instanceCount);
}
}
var instance = new SomeClass();
// console output:
// before change: 0
// after change: 1