「UNSAFE_componentWillMount を厳密モードで使用することは推奨されません。コードにバグがある可能性があります。」を修正するにはどうすればよいですか? 質問する

「UNSAFE_componentWillMount を厳密モードで使用することは推奨されません。コードにバグがある可能性があります。」を修正するにはどうすればよいですか? 質問する

私は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)

このエラーを修正するにはどうすればいいですか?

ベストアンサー1

私の場合、問題はヘルメットのせいだと考えています。"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>
)
}

修正されたコード

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


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

)
}

おすすめ記事