Probably it's natural that everyone starting with React, experiences confusion between props and state.
There is a common distinction that if a component needs to change one of its attributes at some point in time, state should be used, otherwise it should be a prop.
I came up to a point where I decided to implement a copyright date in the footer. You can see it in the example bellow:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
copyrightYear: new Date("2019").getFullYear(),
currentYear: new Date().getFullYear()
};
}
render() {
return (
<div className="container">
<Header />
<MainContent />
<Footer
handleCopyrightDate={() => {
if (this.state.currentYear === this.state.copyrightYear) {
return (
<h3>
Copyright © {this.state.copyrightYear}. Company Name
</h3>
);
} else if (this.state.currentYear > this.state.copyrightYear) {
return (
<h3>
Copyright © {this.state.copyrightYear} -
{this.state.currentYear}. Company Name
</h3>
);
}
}}
/>
</div>
);
}
}
const Footer = props => {
return (
<footer className="footer">
<div className="copyright">{props.handleCopyrightDate()}</div>
</footer>
);
};
const appRoot = document.querySelector("#app");
ReactDOM.render(<App />, appRoot);
In the beginning I decided to move on with a state, but now I start to doubt, does this small code snippet should really be in need of a state? If yes, where should I include setState? Should I create setInterval and make it change every year or should I pass it through props?
I just one the start year or copyright year to be a constant value and the current year to autoupdate every time the year changes.