Kay*_*ote 16 javascript forms components input reactjs
该InputField&Button是进入一个表单创建一个表单自定义组件.我的问题是如何将数据重新发送到表单,以便在按钮单击时,我可以使用数据(用户名和密码)在表单上激活ajax:
export default auth.authApi(
class SignUpViaEmail extends Component{
constructor(props){
super(props);
this.state = {
email : "",
password : ""
};
this.storeEmail = this.storeEmail.bind( this );
this.storePassword = this.storePassword.bind( this );
}
storeEmail(e){
this.setState({ email : e.target.value });
}
storePassword(e){
this.setState({ password : e.target.value });
}
handleSignUp(){
this.props.handleSignUp(this.state);
}
render(){
return(
<div className="pageContainer">
<form action="" method="post">
<InputField labelClass = "label"
labelText = "Username"
inputId = "signUp_username"
inputType = "email"
inputPlaceholder = "registered email"
inputClass = "input" />
<Button btnClass = "btnClass"
btnLabel = "Submit"
onClickEvent = { handleSignUp } />
</form>
</div>
);
}
}
);
Run Code Online (Sandbox Code Playgroud)
或者不建议我不应该在表单中创建自定义子组件?
子组件=> InputField
import React,
{ Component } from "react";
export class InputField extends Component{
constructor( props ){
super( props );
this.state = {
value : ""
};
this.onUserInput = this.onUserInput.bind( this );
}
onUserInput( e ){
this.setState({ value : e.target.value });
this.props.storeInParentState({[ this.props.inputType ] : e.target.value });
}
render(){
return <div className = "">
<label htmlFor = {this.props.inputId}
className = {this.props.labelClass}>
{this.props.labelText}
</label>
<input id = {this.props.inputId}
type = {this.props.inputType}
onChange = {this.onUserInput} />
<span className = {this.props.validationClass}>
{ this.props.validationNotice }
</span>
</div>;
}
}
Run Code Online (Sandbox Code Playgroud)
错误:我e.target is undefined在父storeEmail func上收到错误.
小智 43
React的单向数据绑定模型意味着子组件无法将值发送回父组件,除非明确允许这样做.React的做法是将回调传递给子组件(参见Facebook的"表单"指南).
class Parent extends Component {
constructor() {
this.state = {
value: ''
};
}
//...
handleChangeValue = e => this.setState({value: e.target.value});
//...
render() {
return (
<Child
value={this.state.value}
onChangeValue={this.handleChangeValue}
/>
);
}
}
class Child extends Component {
//...
render() {
return (
<input
type="text"
value={this.props.value}
onChange={this.props.onChangeValue}
/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,父组件处理状态,而子组件仅处理显示.Facebook的"提升状态"指南是学习如何做到这一点的好资源.
这样,所有数据都存在于父组件(处于状态)中,并且子组件仅被赋予更新该数据的方式(作为props传递的回调).现在您的问题得到了解决:您的父组件可以访问它所需的所有数据(因为数据存储在状态中),但您的子组件负责将数据绑定到它们各自的元素,例如<input>标记.
Ali*_*mad 11
父.js
import React, { Component } from 'react';
import Child from './child'
class Parent extends Component {
state = {
value: ''
}
onChangeValueHandler = (val) => {
this.setState({ value: val.target.value })
}
render() {
const { value } = this.state;
return (
<div>
<p> the value is : {value} </p>
<Child value={value} onChangeValue={this.onChangeValueHandler} />
</div>
);
}
}
export default Parent;
Run Code Online (Sandbox Code Playgroud)
儿童.js
import React, { Component } from 'react';
class Child extends Component {
render() {
const { value , onChangeValue } = this.props;
return (
<div>
<input type="text" value={value} onChange={onChangeValue}/>
</div>
);
}
}
export default Child;
Run Code Online (Sandbox Code Playgroud)
你可以看到一个活生生的例子:https : //codesandbox.io/s/two-way-binding-qq1o1?from-embed
| 归档时间: |
|
| 查看次数: |
25445 次 |
| 最近记录: |