I have started learning React and I am not able to understand why is the console.log logging twice. Below is my code and when I inspect it in chrome it shows the word 'random-text' twice instead of once.
import React, { Component } from "react";
class Counter extends Component {
state = {
count: 0
};
render() {
console.log('random-text');
return (
<div>
<span className={this.getBaadgeClasses()}>{this.formatCount()}</span>
<button
onClick={this.handleIncrement}
className="btn btn-success m-2"
>
Increment
</button>
{/* <ul>
{this.state.tags.map((tag) => (
<li key={tag}>{tag}</li>
))}
</ul> */}
</div>
);
}
getBaadgeClasses() {
let classes = "badge m-2 badge-";
classes += this.state.count === 0 ? "warning" : "primary";
return classes;
}
formatCount() {
const { count } = this.state;
return count === 0 ? "Zero" : count;
}
handleIncrement = () => {
this.setState({count: this.state.count + 1})
}
}
export default Counter;
The render function is a lifecycle function, called during the "render phase"
react lifecycle methods diagram
Notice that these functions are pure functions, and can be paused, aborted, or restarted by React. This means react can call render almost any number of times to reconcile the virtualDOM with the actual DOM.
Detecting unexpected side effects
Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:
- Class component
constructor
,render
, andshouldComponentUpdate
methods- Class component static
getDerivedStateFromProps
method- Function component bodies
- State updater functions (the first argument to
setState
)- Functions passed to
useState
,useMemo
, oruseReducer
Note:
This only applies to development mode. Lifecycles will not be double-invoked in production mode.
If you truly want a one-to-one console log when the component updates use one of the other lifecycle functions, like componentDidUpdate
to do the side-effect of logging details.
class Counter extends Component {
state = {
count: 0
};
componentDidUpdate() {
console.log("random-text");
}
render() {
return (
<div>
<span className={this.getBaadgeClasses()}>{this.formatCount()}</span>
<button onClick={this.handleIncrement} className="btn btn-success m-2">
Increment
</button>
{/* <ul>
{this.state.tags.map((tag) => (
<li key={tag}>{tag}</li>
))}
</ul> */}
</div>
);
}
getBaadgeClasses() {
let classes = "badge m-2 badge-";
classes += this.state.count === 0 ? "warning" : "primary";
return classes;
}
formatCount() {
const { count } = this.state;
return count === 0 ? "Zero" : count;
}
handleIncrement = () => {
this.setState({ count: this.state.count + 1 });
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With