如何修复“不建议在严格模式下使用 UNSAFE_componentWillMount,这可能表明代码中存在错误。”?

Mah*_*aji 20 reactjs redux 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);

Run Code Online (Sandbox Code Playgroud)

但我在控制台中收到此错误,该错误来自反应严格模式:

 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)
Run Code Online (Sandbox Code Playgroud)

我该如何修复这个错误?

Yus*_*han 32

就我而言,我认为问题是由于头盔造成的"react-helmet"

解决我使用的问题react-helmet-async并将所有内容包装在<HelmetProvider>jsx return 下

下面是示例代码

有问题的代码

import React from "react";
import { Helmet } from "react-helmet";

function XYZ(){
return(
 <div>
   <Helmet>
     <meta />
   </Helmet>
 
   <p>...</p>
 </div>
)
}
Run Code Online (Sandbox Code Playgroud)

修复代码

import React from "react";
import { Helmet, HelmetProvider } from 'react-helmet-async';


function XYZ(){
return(
<HelmetProvider>
  <div>
    <Helmet>
      <meta />
    </Helmet>
 
    <p>...</p>
  </div>
</HelmetProvider>

)
}
Run Code Online (Sandbox Code Playgroud)


Mah*_*aji 5

正如apokryfos评论的那样,这似乎是一个悬而未决的问题。我应该等待 redux-form 的作者发布更新或寻求替代库(因为这个库的作者似乎说我们在大多数情况下不应该使用它)。