gpt4 book ai didi

javascript - 重构 componentWillReceiveProps 为 Hook

转载 作者:行者123 更新时间:2023-11-30 06:14:10 25 4
gpt4 key购买 nike

我正在使用 Formik 效果库来控制我的 onChange 效果。但是 Formik 效果库坏了,所以我需要编写自己的效果来触发 Formik onChange()功能。我写了一个class我的 <Effect /> 的 componentWillReceiveProps 组件.但是,我所有的组件都是函数组件。我被要求重构我的 class组件到功能组件。我还是 React 的初学者,所以我很纠结如何重构它。

这是我的课<Effect />组件

// TODO: refactor with HOOK!

import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'formik';

class Effect extends React.Component {
componentWillReceiveProps(nextProps) {
const { formik } = this.props;
const { onChange } = this.props;
const { values, touched, errors, isSubmitting } = formik;
const {
values: nextValues,
touched: nextTouched,
errors: nextErrors,
isSubmitting: nextIsSubmitting
} = nextProps.formik;
if (nextProps.formik !== formik) {
onChange(
{
values,
touched,
errors,
isSubmitting
},
{
values: nextValues,
touched: nextTouched,
errors: nextErrors,
isSubmitting: nextIsSubmitting
}
);
}
}

// eslint-disable-next-line
render() {
return null;
}
}

Effect.defaultProps = {
formik: {
errors: {},
touched: {},
values: {},
isSubmitting: false
},
onChange: () => {}
};

Effect.propTypes = {
formik: PropTypes.shape({
errors: PropTypes.shape({}),
touched: PropTypes.shape({}),
values: PropTypes.shape({}),
isSubmitting: PropTypes.bool
}),
onChange: PropTypes.func
};

export default connect(Effect);

在我之内,我归来

<Effect
onChange={(currentFormikState, nextFormikState) => {
if (
currentFormikState &&
nextFormikState &&
currentFormikState.values.pdsaType !==
nextFormikState.values.pdsaType
) {
resetForm(initialValues[nextFormikState.values.type]);
setType(nextFormikState.values.type);
}
}}
/>

此效果的目的是在用户选择不同类型的表单时重置 initialValue,因为 initialValue 仅呈现一次。并且当前的 Formik 库不支持 onChange() 函数。

currentFormikState是 Formik 渲染的初始值,nextFormikStatetype用户选择的值。

最佳答案

我不知道 formik 但我猜从你的代码中你可以像这样转换为函数组件

import React, { useEffect, useRef } from "react";

function Effect(props) {
const { formik, onChange } = props;
const { values, touched, errors, isSubmitting } = formik;
const formVariableRef = useRef();
const formFormikRef = useRef();
useEffect(() => {
formFormikRef.current = formik;
formVariableRef.current = { values, touched, errors, isSubmitting };
if (formik !== formFormikRef) {
onChange(
{
value: formVariableRef.current.values,
touched: formVariableRef.current.touched,
errors: formVariableRef.current.errors,
isSubmitting: formVariableRef.current.isSubmitting
},
{ values, touched, errors, isSubmitting }
);
}
return function() {
formFormikRef.current = formik;
formVariableRef.current = { values, touched, errors, isSubmitting };
};
}, [values, touched, errors, isSubmitting, formik]); //try add this
return null
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

试试用这个代替你的类(class)表格。其余的可能不需要更改。

已更新尝试添加上面的代码。我只是将一些依赖项添加到 useEffect

关于javascript - 重构 componentWillReceiveProps 为 Hook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57245543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com