[英]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.