简体   繁体   English

将道具从孩子传递给父母不工作(反应)

[英]Passed prop from child to parent not working (React)

I have child component with <form> Value from input. 我有来自输入的<form>值的子组件。 Needs to be passed and rendered in parent component. 需要在父组件中传递和呈现。 has onSubmit method which works in child, but doesn't seem to work in parent component. 具有onSubmit方法,该方法适用于子级,但似乎不适用于父级组件。 What is wrong here? 怎么了

I am trying to render value of <input> (child) in <h1> (parrent). 我正在尝试在<h1> (父级)中呈现<input> (子级)的值。

Child.js Child.js

   state = {
     title : ''
   }
  onSubmit = (e) => {
    e.preventDefault();
    this.props.textChange(this.state.title);
    this.setState({ title: '' });
  };

   onChange = (e) => this.setState({ title: e.target.value });


  render() {
    return (
      <div>
      <form 
        onSubmit={this.props.onSubmit}>
        <input 
          type="text" 
          name="title"
          placeholder="your tex here..."
          value={this.state.title}
          onChange={this.onChange}
        />

        <input 
        type="submit"
        value ="Submit"
        />
        </form>
      </div>
    )
  }
}
export default Text

Parent.js Parent.js

class App extends Component {
  state = {
    colors: [],
    text: 'title'
  };

textChange = (title) => {
  console.log(title);
  this.setState({ text: title });
}

 return (
    <div className="App">
      <h1 className="title">{this.state.text}</h1>
      <Text textChange={this.textChange}/>
    </div>

props are used to access parent method/values 道具用于访问父方法/值

change {this.props.onSubmit} to {this.onSubmit} as onSubmit is a method of the child {this.props.onSubmit}更改为{this.onSubmit}因为onSubmit是子方法

Remove props from onSubmit={this.props.onSubmit} . onSubmit={this.props.onSubmit}移除props Props are used to access data from the parent. 道具用于从父级访问数据。 In this case, the onSubmit function is local to the Child.js file, not the Parent.js file. 在这种情况下, onSubmit函数在Child.js文件而不是Parent.js文件本地。

state = {
  title: ''
}
onSubmit = (e) => {
  e.preventDefault();
  this.props.textChange(this.state.title);
  this.setState({
    title: ''
  });
};

onChange = (e) => this.setState({
  title: e.target.value
});


render() {
  return ( <
    div >
    <
    form onSubmit = {
      this.onSubmit // REMOVED .props
    } >
    <
    input type = "text"
    name = "title"
    placeholder = "your tex here..."
    value = {
      this.state.title
    }
    onChange = {
      this.onChange
    }
    />

    <
    input type = "submit"
    value = "Submit" /
    >
    <
    /form> < /
    div >
  )
}
}
export default Text

You are trying to call onSubmit method from props, having this method in child component. 您正在尝试从道具中调用onSubmit方法, onSubmit此方法放在子组件中。 Instead you should call it directly on this object. 相反,您应该直接在this对象上调用它。 Like this: 像这样:

<form 
  onSubmit={this.onSubmit}>

you are accessing props which is not being passed to child component, you should change the the this.props.onSubmit to this.onSubmit. 您正在访问未传递给子组件的道具,则应将this.props.onSubmit更改为this.onSubmit。

state = {
     title : ''
   }
  onSubmit = (e) => {
    e.preventDefault();
    this.props.textChange(this.state.title);
    this.setState({ title: '' });
  };

   onChange = (e) => this.setState({ title: e.target.value });


  render() {
    return (
      <div>
      <form 
        onSubmit={this.onSubmit}>
        <input 
          type="text" 
          name="title"
          placeholder="your tex here..."
          value={this.state.title}
          onChange={this.onChange}
        />

        <input 
        type="submit"
        value ="Submit"
        />
        </form>
      </div>
    )
  }
}
export default Text

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

相关问题 传递了字符串属性以响应父级的子级组件-无法使用JSON.parse()转换为Object - String prop passed to react child component from parent - unable to conver to Object with JSON.parse() React - 使用从父组件传递的道具渲染子组件时出错 - React - Error while rendering child component with prop passed from parent component 使用React和Gatsby将道具从父母传递给孩子 - Passing a prop to a child from parent with React and Gatsby 将支持从孩子传给父母React - Pass prop from child to parent React 反应从孩子到父母的更新数组道具 - React update array prop from child to parent 如何在 React 中将整数值从父道具传递给子道具 - How to pass integer value from parent prop to child prop in React 在从父级作为道具传递的子级中调用方法时遇到问题 - Having trouble invoking method in child that is passed as prop from parent 测试作为道具从父级传递给子级的异步函数 - testing the async function passed as a prop from parent to child React中从父组件传递给子组件的“未定义”? - 'Undefined' passed from parent to child component in React? React-Slick轮播-在父级组件的prop触发下,自动播放在子级组件中不起作用 - React-slick carousel - autoplay not working in child component while triggering it with prop from parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM