简体   繁体   English

如何将道具从自动更正传递给父母 class

[英]How to pass props from autocorrect to parent class

I've previously asked a question regarding the passing of state for react through props here: Laggy TextField Updates in React .我之前已经问过一个关于传递 state 以通过道具做出反应的问题: Laggy TextField Updates in React

I've decided to refactor my code using ChrisG's method, where I store states in the parent FormSection, and pass props into the child CompanyField.我决定使用 ChrisG 的方法重构我的代码,我将状态存储在父 FormSection 中,并将道具传递给子 CompanyField。

Parent:家长:

class FormSection extends React.Component {

  state = { 
    company: '', 
    jobType: ''
  };

  createHandleChange(name) {
    return (e) => {
      try {
      this.setState({ [name]: e.target.value.toLowerCase() }); //convert to lowercase if text data
      } catch {
        this.setState({ [name]: e.target.value }); 
      }
      console.log(name,  e.target.value);
    };
  }

  render() {
    return (
        <FormContainer>
            <CompanyField  # Working Correctly
                value={this.state.company}
                handleChange={this.createHandleChange("company")}
            />

            <JobTypeField  # Does not work
                value={this.state.jobType}
                handleChange={this.createHandleChange("jobType")}
            />
        </FormContainer>
   )
}

TextFieldStyled is just MUI's textfield that was styled using styled components. TextFieldStyled 只是 MUI 的文本字段,它使用样式化组件进行了样式化。

Child:孩子:

    class CompanyField extends React.Component {
    
      render() {
        return (
          <TextFieldStyled
            id = "outlined-basic" 
            label = "Company" 
            variant = "outlined"
            fullWidth
    
            value={this.props.value}
            onChange={this.props.handleChange}
          />
        );
      }
    }

This managed to successfully update the parent components state. However, when doing the same thing to MUI's autocomplete:这成功地更新了父组件 state。但是,当对 MUI 的自动完成执行相同的操作时:

class  JobTypeField extends React.Component {
    
    render() {
        return (
            <DropDownStyled>
                <Autocomplete
                    disablePortal
                    id="combo-box-demo"
                    options={jobType}

                    value={this.props.value}
                    onChange={this.props.handleChange}
                    renderInput={(params) => <TextField {...params} label="Job Type" />}
                />
            </DropDownStyled>
        );
    }
}

const jobType = [
  { label: 'Full-Time' },
  { label: 'Part-Time' },
  { label: 'Internship' },
  { label: 'Contract' },
  { label: 'Freelance' },
];

This error occurs: The value provided to Autocomplete is invalid.发生此错误:提供给自动完成的值无效。 None of the options match with 0 .没有一个选项与0匹配。 You can use the isOptionEqualToValue prop to customize the equality test.您可以使用isOptionEqualToValue来自定义相等性测试。

After clicking an option, it defaults to 0.单击一个选项后,它默认为 0。

下拉菜单

单击选项后

Additionally, the state is not updated.此外,state 未更新。

Thanks!谢谢!

Resolved: I've realised that unlike the textfield that returns one event prop whereby I have to do e.target.value, the MUI textfield returns two props namely event.已解决:我已经意识到,与返回一个事件道具的文本字段不同,我必须执行 e.target.value,MUI 文本字段返回两个道具,即事件。 As such, I have to create a second change handler that accepts 2 props event and value, but only using the value prop instead.因此,我必须创建第二个更改处理程序,它接受 2 个 props 事件和值,但只使用 value prop。

  createHandleChange2(name) {
    return (event, value) => {
      try {
        this.setState({ [name]: value.label.toLowerCase() }); 
        console.log(name,  value.label);  
      } catch {
        this.setState({ [name]: '' }); 
        console.log(name,  '');
      }
    };

Does anyone know of a way to combine common handling events whereby they have different input (namely text field that has 1, vs autocorrect that has 2)?有谁知道一种方法来组合常见的处理事件,从而使它们具有不同的输入(即具有 1 的文本字段与具有 2 的自动更正)? Or do I have to do some kind of overloading method?或者我必须做某种重载方法吗?

"e" is undefined in your createHandleChange method. “e”在您的createHandleChange方法中未定义。 Try to get a reference to it by updating your code as below.尝试通过如下更新代码来获取对它的引用。

    ...
    createHandleChange(e, name) {
      return (e) => {
        try {
          this.setState({ [name]: e.target.value.toLowerCase() }); //convert to lowercase if text data
        } catch {
          this.setState({ [name]: e.target.value }); 
        }
        console.log(name,  e.target.value);
      };
    }
    ...

    <CompanyField  // Working Correctly
        value={this.state.company}
        handleChange={(e) => this.createHandleChange(e, "company")}
    />

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

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