如何修复`不建议在严格模式下使用 UNSAFE_componentWillMount,这可能表明您的代码中存在错误。`?
How can I fix `Using UNSAFE_componentWillMount in strict mode is not recommended and may indicate bugs in your code.`?
我在 React 项目中使用 redux 表单,这是初始化了 redux 表单的应用程序组件:
import { Field, reduxForm } from 'redux-form';
const onSubmit = (values) => {
alert(JSON.stringify(values));
};
function App(props) {
return (
<div className="App">
<form onSubmit={props.handleSubmit}>
<div>
<label htmlFor="firstName">First Name</label>
<Field name="firstName" component="input" type="text" />
</div>
<div>
<label htmlFor="lastName">Last Name</label>
<Field name="lastName" component="input" type="text" />
</div>
<div>
<label htmlFor="email">Email</label>
<Field name="email" component="input" type="email" />
</div>
<button type="submit">Submit</button>
</form>
{props.number}
<button onClick={() => props.callAction()} />
</div>
);
}
App = reduxForm({
form: 'contact',
onSubmit
})(App);
但是我在控制台中收到这个错误,它来自 react strict 模式:
Using UNSAFE_componentWillReceiveProps in strict mode is not recommended and may indicate bugs in your code.
* Move data fetching code or side effects to componentDidUpdate.
* If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at:state
Please update the following components: Field, Form(App)
我该如何解决这个错误?
至于apokryfos commented, there seems to be an open issue这个。我要么等待 redux-form 的作者发布更新,要么寻找替代库(因为这个库的作者似乎说在大多数情况下我们不应该使用它)。
我在 React 项目中使用 redux 表单,这是初始化了 redux 表单的应用程序组件:
import { Field, reduxForm } from 'redux-form';
const onSubmit = (values) => {
alert(JSON.stringify(values));
};
function App(props) {
return (
<div className="App">
<form onSubmit={props.handleSubmit}>
<div>
<label htmlFor="firstName">First Name</label>
<Field name="firstName" component="input" type="text" />
</div>
<div>
<label htmlFor="lastName">Last Name</label>
<Field name="lastName" component="input" type="text" />
</div>
<div>
<label htmlFor="email">Email</label>
<Field name="email" component="input" type="email" />
</div>
<button type="submit">Submit</button>
</form>
{props.number}
<button onClick={() => props.callAction()} />
</div>
);
}
App = reduxForm({
form: 'contact',
onSubmit
})(App);
但是我在控制台中收到这个错误,它来自 react strict 模式:
Using UNSAFE_componentWillReceiveProps in strict mode is not recommended and may indicate bugs in your code.
* Move data fetching code or side effects to componentDidUpdate.
* If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at:state
Please update the following components: Field, Form(App)
我该如何解决这个错误?
至于apokryfos commented, there seems to be an open issue这个。我要么等待 redux-form 的作者发布更新,要么寻找替代库(因为这个库的作者似乎说在大多数情况下我们不应该使用它)。