简体   繁体   English

父组件向其传递新道具后,子组件不会更新

[英]Child Component is not updating after Parent Component passes it new props

When I get userInput from onChange() and try to pass that to the child Component It is not updating rather holding on to the initial value.当我从 onChange() 获取 userInput 并尝试将其传递给子组件时,它不会更新而是保持初始值。 I'm trying to pass the string from input field to child component called Tensor-flow Toxic Model, however the state of TensorFlowToxidModel does not change after it is initially set.我试图将字符串从输入字段传递到名为 Tensor-flow Toxic Model 的子组件,但是 TensorFlowToxidModel 的状态在初始设置后不会改变。 So I cant run the moddl.所以我无法运行 moddl。

class TensorFlowToxicModel extends React.Component<{}, ToxicityModelProp> {
  constructor(props: userInput) {
    super(props);
    this.state = {
      modelObjectArray: [],
      userSentence: props.userSentence,
    };
  }      
  componentDidUpdate(){
    console.log("This is from TensorFlowToxicModel Compononent")
    console.log("This is the sentence ", this.state.userSentence )
  }
  renderThePost = () => {
    let output = cleanMlOutput(this.state.userSentence)
    return output
  };
  render() {
    return (
      <div>
        <p>This is a Checker Does this even work</p>
      </div>
    );
  }
}

class InputField extends React.Component<{}, userInput> {
  constructor(prop: inputFromField) {
    super(prop);
    this.state = {
      userSentence: "",
    };
  }
 
  handleChange = (event: React.FormEvent<HTMLInputElement>): void => {
    let userInputData: string = event.currentTarget.value;
    //console.log(event.currentTarget.value);
    
    this.setState({
      userSentence: userInputData,
    }); 
  };

  render() {
    const userSentence = {
      userSentence:this.state.userSentence
    }
    //Instead of updating TensorFlowToxicModel Each time from inside its own compnoent 
    //Call it here each time user types something
    return (
      <div>
        <input id="inputField" onChange={this.handleChange} />
        <h4>{this.state.userSentence}</h4>
        
        <TensorFlowToxicModel {...userSentence}/>
        
      </div>
    );
  }
}

the Types类型

    type modelObject = { label: string; resultMatch: boolean; resultProbablity: number; }; 
type ToxicityModelProp = { userSentence: string; modelObjectArray : modelObject[] } 

You're misplaced the prop types ToxicityModelProp .你放错了prop类型ToxicityModelProp It should be on first.应该先开。 Read this docs for information about component props,state types阅读此文档以获取有关组件道具、状态类型的信息

type ToxicityModelProp = { userSentence: string } 
type ToxicityModelState = { modelObjectArray: [] }


class TensorFlowToxicModel extends React.Component<ToxicityModelProp, ToxicityModelState> {
  constructor(props: userInput) {
    super(props);
    this.state = {
      modelObjectArray: []
    };
  } 
  renderThePost = () => {
    let output = cleanMlOutput(this.props.userSentence)
    return output
  };
  render() {
    return (
      <div>
        <p>Sentence is: { this.props.userSentence }</p>
      </div>
    );
  }
}

I have made some changes on your code and update here .我对您的代码进行了一些更改并在此处更新。 Check it out一探究竟

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

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