getderivedstatefromprops

Why getDerivedStateFromProps is called after setState?

给你一囗甜甜゛ 提交于 2019-12-21 06:48:54
问题 React introduced new static method getDerivedStateFromProps(props, state) which is called before every render method, but why? Calling it after prop change makes sense to me but after setState it doesn't, maybe I am missing something. I was creating a datePicker Component according to my company requirement, in the component date is controlled from the prop. I have the following state in the component. selectedDate: number; selectedMonth: number; selectedYear: number; currentMonth: number;

ReactJS: Save input value on blur and not on every key stroke

 ̄綄美尐妖づ 提交于 2019-12-08 03:58:01
问题 I have created a React View, say MyView , which has 2 text inputs whose initial values will be passed by parent read from a DB. I also want the changed values to be saved back to DB. So, the view is also passed a callback function for the same. Consider that DB save operation is heavy and you should not do it very frequently . So, I decided to listen to onBlur events instead of onChange events on the input boxes as onChange is invoked on every key stroke. First Approach: class MyView extends

ReactJS: Save input value on blur and not on every key stroke

。_饼干妹妹 提交于 2019-12-06 21:45:33
I have created a React View, say MyView , which has 2 text inputs whose initial values will be passed by parent read from a DB. I also want the changed values to be saved back to DB. So, the view is also passed a callback function for the same. Consider that DB save operation is heavy and you should not do it very frequently . So, I decided to listen to onBlur events instead of onChange events on the input boxes as onChange is invoked on every key stroke. First Approach: class MyView extends React.Component { constructor(props) { super(props); } render() { return ( <div> <input type="url"