I'm new to react and I'm still using the life cycle methods, so my question is since componentDidMount()
acts like a constructor I'm planning to initialize everything there:
render() { return ( <div className="done-container" style={this.style().taskContainer}> <span style={this.style().title}>Done</span> { this.props.done.map((d) => ( <div className={`done ${this.state.isActive ? 'active' : ''}`} id={this.props.done.id} onClick={(e) => { this.props.setTaskID(d.id); this.setToActive(e); //3.Call <----- this.props.arrowStyleToDone(); }}> </div> )) } </div> ) } setToActive //1.Declare <---- componentDidMount() { //2.Initialize <----- this.setToActive = (e) => { if(!e.target.classList.contains('active')) { e.currentTarget.classList.add('active'); e.currentTarget.classList.add('border-done') this.props.closeAllTasks(); this.props.closeAllDoing(); } else { e.currentTarget.classList.remove('active'); e.currentTarget.classList.remove('border-done') this.props.disableArrowButton(); } } } }
My idea is that if I declare everything inside the class which looks very ugly in my opinion, but then initializing everything on componentDidMount()
my web app will be faster because it won't need to declare and initialize everything every render.
Is this correct? and should I put the declaring and initialization on top of render()
? but the componenDidMount()
is called after the initial render.