1. LocalStorage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>ReactJS</title> </head> <body> <div id="root"></div> <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <script type="text/babel"> class App extends React.Component { constructor(props) { super(props); this.state = { id: 0, username: 'Loading...', }; } //sử dụng setState khi get được localstorage trong componentWillMount() componentDidMount() { if (localStorage && localStorage.getItem('username')) { this.setState({ username: JSON.parse(localStorage.getItem('username')), }) } } componentDidUpdate(prevProps, prevState) { if (prevState.username !== this.state.username) { localStorage.setItem('username', JSON.stringify(this.state.username)); } } render() { return ( <div> <input type="text" name="username" value={this.state.username} onChange={(e) => { this.setState({ username: e.target.value }) }} /> <h4> - Username: {this.state.username}</h4> </div> ); } } ReactDOM.render(<App />, document.getElementById('root')); </script> </body> </html> |
2. getAPI in Lifecycle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>ReactJS</title> </head> <body> <div id="app"></div> <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react.js"></script> <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react-dom.js"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <script type="text/babel"> class Student extends React.Component { constructor(props) { super(props); this.isComponentMounted = false; this.state = { student: { name: 'Loading...', email: 'Loading...', }, }; } componentDidMount() { this.isComponentMounted = true; this.getStudent(1); } componentDidUpdate(prevProps, prevState) { if (prevProps.id == this.props.id) return; this.getStudent(this.props.id); } componentWillUnmount() { this.isComponentMounted = false; } async getStudent(id) { try { let student = await this.api_Student(id); //Khi component bị unmount rồi thì sẽ không setState được nữa if (this.isComponentMounted) { this.setState({ student: student, }); } } catch (error) { console.log(error); } } api_Student(id) { // Mình sẽ giả lập gọi api getStudent bằng hàm setTimeout nhé // Hành động thực tế ở đây là sẽ phải gọi api để lấy thông tin sinh viên theo id nha // Và Sau khi lấy được thông tin thì trả về thông tin sinh viên get được return new Promise((resolve, reject) => { setTimeout(() => { if (1 == 1) { let student = { name: 'John Baker - ' + id, }; return resolve(student); } reject("Can't get Student"); }, 1000); }); } render() { return ( <div> <p>ID: <strong>{this.props.id}</strong></p> <p>Name: <strong>{this.state.student.name}</strong></p> <p>Email: <strong>{this.state.student.email}</strong></p> </div> ); } } class App extends React.Component { constructor(props) { super(props); this.state = {id: 0}; } render() { return ( <div> <input type="text" name="id" value={this.state.id} onChange={(e) => { this.setState({ id: e.target.value }); }} /> <Student id={this.state.id} /> </div> ); } } ReactDOM.render(<App />, document.getElementById('app')); </script> </body> </html> |