简体   繁体   English

如何使用react和typescript根据formfield的值将react state设置为一些初始state?

[英]How to set react state to some initial state based on the value of formfield using react and typescript?

i want to set state to initial state based on some condition using react.我想根据使用反应的某些条件将 state 设置为初始 state。

what i am trying to do?我想做什么?

i have a checkfield named active.我有一个名为 active 的复选框。 if user checks active field then the form field named active should be set to value which is based on option selected in the select menu.如果用户检查活动字段,则名为活动的表单字段应设置为基于 select 菜单中选择的选项的值。

basically it is like below,基本上就像下面一样,

在此处输入图像描述 so as shown in above picture, the check field will have form field named 'active'.如上图所示,检查字段将具有名为“活动”的表单字段。 if it is unchecked then the select menu is not visible and the form field with name active will be set with value 'none'.如果未选中,则 select 菜单不可见,并且名称为 active 的表单字段将设置为值“none”。

if it is checked, the select menu is visible and the form field with name active will be set to 'type2' if the type2 valueit will be set to 'type1' if the type1 option is selected from menu.如果选中,则 select 菜单可见,如果 type2 值,则名称为 active 的表单字段将设置为“type2”,如果从菜单中选择了 type1 选项,则将设置为“type1”。

below is my code,下面是我的代码,

const Parent = () => { 
    const [isChecked, setIsChecked] = React.useState(false);
    return (
        <Switch
            checked={isChecked}
            disabled={mode === 'edit'}
            onChange=(() => {
                if (isChecked) {
                    setIsChecked(false);
                    form.setFieldValue('active', 'none');
                } else {
                    setIsChecked(true);
                }
            }
        />
    );
}


    
const Child = () => {
    
    const handleOnchange = (selectedType, form) => {
        if (selectedType === 'type1') {
            form.setFieldValue('active', 'type1');
        } else if (selectedType === 'type2') {
            form.setFieldValue('active', 'type2');
        } else {
            //do nothing
        }
    }
    const typeOptions = () => {
        label: 'type1', name:'type1',
        label: 'type2', name: 'type2',
    }
    return ( 
        <FormField fieldId="type">
            {({ field, form }: FieldProps) => (
                <Select 
                    options={typeOptions}
                    onChange=(()=> handleOnChange(type,form));
                    placeholder="select"
                    value=value={typeOptions.filter(option => option.value === 
                        field.value)}
                 /> 
             ))}
         </FormField>
     );
 }

The above code works.上面的代码有效。 but the same Parent component used in create and edit mode.但在创建和编辑模式中使用相同的父组件。 when in edit mode i want the active field to be checked if the active field value is type1 or type2 in create mode.and unchecked if the active field value is set to none in create mode.在编辑模式下,如果活动字段值在创建模式下是 type1 或 type2,我希望检查活动字段。如果在创建模式下活动字段值设置为 none,则取消选中。

basically in edit mode i want to retrieve the value of active field.基本上在编辑模式下我想检索活动字段的值。 since i am using isChecked state on switch component checked prop and when the component unmounts it will be set to false and hence the active field is unchecked in edit mode even if it is checked in create mode.因为我在开关组件检查属性上使用 isChecked state 并且当组件卸载时,它将设置为 false ,因此即使在创建模式下检查了活动字段,也不会在编辑模式下检查活动字段。

i have tried something like below,我尝试过类似下面的方法,

const Parent = () => {
    const active = formikBag.values('active');
    const checked = active === 'type1' || 'type2';
    const [isChecked, setIsChecked] = React.useState(checked);
    return(
        <Switch
            disabled={mode === edit}
            checked={isChecked} // true | active is not assigned to boolean | 
            //undefined
            onChange = (() => {
                setIsChecked={false} // false is not assignable to 
                //setStateAction(true | active)
            } 
        />
    );
}

but i get errors like true |但我得到像 true | 这样的错误active is not assigned to boolean |活动未分配给 boolean | undefined false is not assignable to setStateAction(true | active) undefined false 不可分配给 setStateAction(true | active)

Not sure what is causing this.不确定是什么原因造成的。 could someone help me with this.有人可以帮我解决这个问题。 thanks.谢谢。

Or in other words, how can i set checked variable to true if active variable is 'type1' or 'type2'in the below statement或者换句话说,如果在下面的语句中活动变量是“type1”或“type2”,我如何将检查变量设置为 true

const checked = active === 'type1' || active === 'type2'

There are a lot of ways to set this up, but here's what I did.有很多方法可以设置它,但这就是我所做的。

We are going to have three fields in the form:我们将在表单中包含三个字段:

  1. mode : "view" or "edit" mode :“查看”或“编辑”
  2. checked : true or false checkedtruefalse
  3. active : "type1", "type2", or "none" active :“type1”、“type2”或“none”
type ValidTypes = "type1" | "type2" | "none";

interface FormState {
  mode: "view" | "edit";
  checked: boolean;
  active: ValidTypes;
}

I am relying on the form state for everything that we need, so there is no more useState .我依靠表格 state 来满足我们需要的一切,所以没有更多useState Everything can be accessed from the form state.一切都可以从表格 state 访问。

formik comes in with a built-in helper function handleChange that we can use, as long at the element has the "name" property set to the name of the field. formik带有一个内置的帮助器 function handleChange ,我们可以使用它,只要元素的“名称”属性设置为字段的名称。 We want extra event handling on the checked field because we want to clear the value of active and set it to none when the checkbox is unchecked.我们希望对checked的字段进行额外的事件处理,因为我们希望清除active的值并在未选中复选框时将其设置为none So we call handleChange but we also maybe call setFieldValue .所以我们调用handleChange但我们也可能调用setFieldValue

I am using the Checkbox component from material-ui and the dropdowns are just regular select elements.我正在使用material-ui中的Checkbox组件,下拉列表只是常规的select元素。 I had trouble with the material version.我在材料版本上遇到了麻烦。

The RenderForm component gets all of the properties of form as props . RenderForm组件将form的所有属性作为props获取。 The ones that I am using are values , handleChange , and setFieldValue .我使用的是valueshandleChangesetFieldValue

const RenderForm = ({ values, handleChange, setFieldValue }: FormikProps<FormState>) => {
  
  // for development purposes, log the state of `active`
  console.log("active: " + values.active);

  // show options if in edit mode or if checked is true
  const isShowOptions = values.mode === "edit" || values.checked;

  return (
    <Form>
      <div>
        {"Mode: "}
        <select name="mode" value={values.mode} onChange={handleChange}>
          <option value="view">View</option>
          <option value="edit">Edit</option>
        </select>
      </div>
      <div>
        <Checkbox
          name="checked"
          value={values.checked}
          onChange={(event, isChecked) => {
            // want to handle change for this field, but also want to
            // set `active` to `none` when unchecking
            handleChange(event);
            if (!isChecked) {
              setFieldValue("active", "none");
            }
          }}
        />
        {isShowOptions && ( // render this field conditionally
          <select name="active" value={values.active} onChange={handleChange}>
            <option value="none">Select</option>
            <option value="type1">Type 1</option>
            <option value="type2">Type 2</option>
          </select>
        )}
      </div>
      <div>
        <button type="submit">Submit</button>
      </div>
    </Form>
  );
};

const MyForm = () => {
  return (
    <Formik
      initialValues={{ mode: "view", active: "none", checked: false }}
      onSubmit={console.log}
      component={RenderForm}
    />
  );
};

export default MyForm;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM