The following components will yield the same result:
const currYear = Date.now().getFullYear();
class App extends React.Component {
render() {
return <MyComponent year={currYear} />;
}
};
class App extends React.Component {
constructor() {
super();
this.currYear = Date.now().getFullYear();
}
render() {
return <MyComponent year={this.currYear} />;
}
};
Assume the variable never changes.
Can their application be considered equivalent?
If not, are there situations where one should prefer the one method over the other?
Been curious for a long time, but never found a solid answer.
currYearwill be evaluated as your code is interpreted. In the second case,currYearwill not be evaulated unless until some part of app executesnew App()new App()was called yielding different date! I'd personally go with the 2nd approach