Home > Article > Web Front-end > How to implement custom life cycle in JavaScript (code example)
The content of this article is about the method of implementing custom life cycle in JavaScript (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The popularity of React, Vue and Angular has made the term "life cycle" often appear in the mouths of front-end people, so that one of the most common questions in interviews is also:
Introduction What are the life cycles and usage methods of React and Vue?
The "life cycle" that sounds fancy is actually just some ordinary methods, just passing parameters and calling them at different times. We can simulate a simple class ourselves according to React's life cycle, and let this class have some life cycle hooks
We hope to implement a State class, which has the following methods and life cycle:
Method:
setState
class User extends State { constructor(name) { super(); this.state = { name } } willStateUpdate(nextState) { console.log('willStateUpdate', nextState); } shouldStateUpdate(nextState) { console.log('shouldStateUpdate', nextState); if (nextState.name === this.state.name) { return false; } return true; } didStateUpdate(prevState) { console.log('didStateUpdate', prevState); } } const user = new User('deepred'); user.setState({ name: 'hentai' });First of all, you need to know If you don’t know the basics of JavaScript’s object-oriented knowledge, you can first read this article JavaScript’s object-oriented Implementation of setState
class State { constructor() { this.state = {}; } setState(nextState) { const preState = this.state; this.state = { ...preState, ...nextState, }; } }
class User extends State { constructor(name) { super(); this.state = { name } } } const user = new User('tc'); user.setState({age: 10}); // {name: 'tc', age: 10}In React, the setState method will only change specific The value of the attribute, therefore, we need to use a variable preState in the method to retain the previous state, and then merge the old and new states through the spread operator
Implementation of willStateUpdate
willStateUpdate is called before the state is updated. So just call willStateUpdate before merging state
class State { constructor() { this.state = {}; } setState(nextState) { // 更新前调用willStateUpdate this.willStateUpdate(nextState); const preState = this.state; this.state = { ...preState, ...nextState, }; } willStateUpdate() { // 默认啥也不做 } }
class User extends State { constructor(name) { super(); this.state = { name } } // 覆盖父级同名方法 willStateUpdate(nextState) { console.log('willStateUpdate', nextState); } } const user = new User('tc'); user.setState({age: 10}); // {name: 'tc', age: 10}
Implementation of shouldStateUpdate
We stipulate that state will be updated only when shouldStateUpdate returns true. Therefore, before merging state, shouldStateUpdate
class State { constructor() { this.state = {}; } setState(nextState) { this.willStateUpdate(nextState); const update = this.shouldStateUpdate(nextState); if (update) { const preState = this.state; this.state = { ...preState, ...nextState, }; } } willStateUpdate() { // 默认啥也不做 } shouldStateUpdate() { // 默认返回true,一直都是更新 return true; } }
class User extends State { constructor(name) { super(); this.state = { name } } // 覆盖父级同名方法 willStateUpdate(nextState) { console.log('willStateUpdate', nextState); } // 自定义何时更新 shouldStateUpdate(nextState) { if (nextState.name === this.state.name) { return false; } return true; } } const user = new User('tc'); user.setState({ age: 10 }); // {name: 'tc', age: 10} user.setState({ name: 'tc', age: 11 }); // 没有更新
implementation of didStateUpdate
If you understand willStateUpdate, you will also know how didStateUpdate is implemented
class State { constructor() { this.state = {}; } setState(nextState) { this.willStateUpdate(nextState); const update = this.shouldStateUpdate(nextState); if (update) { const preState = this.state; this.state = { ...preState, ...nextState, }; this.didStateUpdate(preState); } } willStateUpdate() { // 默认啥也不做 } didStateUpdate() { // 默认啥也不做 } shouldStateUpdate() { // 默认返回true,一直都是更新 return true; } }
class User extends State { constructor(name) { super(); this.state = { name } } // 覆盖父级同名方法 willStateUpdate(nextState) { console.log('willStateUpdate', nextState); } // 覆盖父级同名方法 didStateUpdate(preState) { console.log('didStateUpdate', preState); } shouldStateUpdate(nextState) { console.log('shouldStateUpdate', nextState); if (nextState.name === this.state.name) { return false; } return true; } } const user = new User('tc'); user.setState({ age: 10 }); user.setState({ name: 'tc', age: 11 });Passed With just a few dozen lines of code, we have implemented a State class with its own life cycle!
The above is the detailed content of How to implement custom life cycle in JavaScript (code example). For more information, please follow other related articles on the PHP Chinese website!