First, Introducint JSX
Inserting user input in JSX is safe, by default reactdom will escape any value in JSX before rendering, and all values will be converted to string form before rendering, which prevents XSS attacks.
Ii. Components and Props
All React the must act like pure functions with respect to their props.
The props in the component are read-only and cannot be modified
Iii. State and Lifecycle
State is similar to props, but it is private and fully controlled by the component itself. The state attribute only appears in class.
If you don't use something render()
in, it shouldn ' t is in the state.
The only place to specify the This.state value is in constructor.
The state Update may be asynchronous
For better performance, react may invoke an update that batches multiple SetState (). Because This.props and this.state can be updated asynchronously, they cannot rely on their values to calculate the next state value. For example, the following may cause the counter update to fail:
// wrong This . SetState ({ this. Props.increment,});
To solve this problem, you can use SetState () in the second method to pass in a function instead of an object to SetState (). The first parameter of the passed-in function is the current state, and the second parameter is the props before the update. So the above example can be rewritten as:
// Correct this. SetState ((prevstate, props) = ({ + props.increment});
Study Notes on official website