Change detection - mechanism that tracks data changes and notifies appropriate listeners
As an example - look on the BtnWithCounter:
It looks very simple from the first sight but imagine:
🤯🤯🤯
class CalculatorService {
income: number;
setIncome(newIncome){
this.income = newIncome;
}
}
{{income}}
In Angular this works because of NgZone and patching all DOM events
Whenever something happens - click, network, input - Angular starts checking changes
With explicit way you have to somehow notify framework that data changes
Usually this achieved with using special methods
setState({})
const [counter, setCounter] = useState({});
setCounter(1);
const data = { counter: 0 };
const MyBtn = () => {
return <button onClick={() => (data.counter += 1)}>
{data.counter}
</button>;
};
const MyBtn = () => {
const [data] = useState({ counter: 0 });
return <button onClick={() => (data.counter += 1)}>
{data.counter}
</button>;
};
class MyBtn extends Component {
state = { counter: 0 };
render() {
return (
<button onClick={() => (this.state.counter += 1)}>
{this.state.counter}
</button>
);}
}
Using this.setState() with functional components
Using useState with class-based components