[英]Pass values to another component in React Final Form
Using Wizard form example to create component with current input values. 使用向导表单示例创建具有当前输入值的组件。 Strangely enough, components are identical, but only
Wizard
component returns object with initial values, when Slider
component returns empty object. 奇怪的是,组件是相同的,但是当
Slider
组件返回空对象时,只有Wizard
组件返回具有初始值的对象。 Most important, is it possible to keep values updated? 最重要的是,是否可以保持值的更新?
class Slider extends React.Component {
constructor(props) {
super(props)
this.state = {
page: 0,
values: props.initialValues || {}
}
}
render() {
const { values } = this.state
console.log(values);
return (
<div></div>
)
}
}
UPDATE 更新
My problem is input type range styling: https://codesandbox.io/s/w65rq7ok4w . 我的问题是输入类型范围样式: https : //codesandbox.io/s/w65rq7ok4w 。 Trying to create a component that would return div with dynamically changing width that would depend from input type range value eg Input Range progress with css gradient
尝试创建一个将返回div并具有动态变化宽度的组件,该宽度将取决于输入类型范围值,例如具有CSS梯度的Input Range进度
You need to create parent component
of all your child forms which contain state. 您需要创建所有包含状态的子表单的
parent component
。 Here is example: 这是示例:
class Slider extends React.Component {
constructor(props) {
super(props)
this.state = {
page: 0,
values: props.initialValues || {
employed: false,
otherKey: "otherValue"
}
}
this.handleUpdate = this.handleUpdate.bind(this);
this.nexPage = this.nexPage.bind(this);
}
handleUpdate(nextValues) {
this.setState({ values: { ...this.state.values, nextValues} });
}
nextPage() {
this.setState({ page: this.state.page + 1 });
}
renderForm(){
const { values } = this.state;
switch(page) {
case 3: return <ThirdForm {...values}/>;
case 1: return <AnotherForm {...values} />;
default: return <FirstForm {...values}/>;
}
}
render() {
const { values } = this.state
console.log(values);
return (
<div>
{this.renderForm()}
</div>
)
}
}
So your values store in on place, and changes only by handleUpdate
method parents component. 因此,您的值就地存储,并且只能通过
handleUpdate
方法的父级组件进行更改。
Parent component will be pass it data to child component when child component onChange
method fires. 当子组件的
onChange
方法触发时,父组件会将其数据传递给子组件。 Here is example(other forms same...): 这是示例(其他形式相同...):
class FirstForm extends React.Component {
handleChange(e) {
this.props.handleUpdate({ [e.target.name]: e.target.value });
}
handleSubmit(e) {
e.preventDefault();
this.props.nextPage();
}
render() {
const { value1 } = this.props;
return(
<form onSubmit={this.handleSubmit.bind(this)}>
<input name="value1" value={value1} onChange={this.handleChange.bind(this)}/>
</form>
);
}
}
Set up attributes name
is a key and value
as value to your inputs and pass it to parent component through childs handleChange
method and parents handleUpdate
method. 设置属性
name
是输入的value
的键和value
,并将其通过子级handleChange
方法和父级handleUpdate
方法传递给父级组件。
When submit fires, you need to change page by parent method nextPage
. 提交时,您需要通过父方法
nextPage
来更改页面。
For input width styling(range min[0] max[100]): 对于输入宽度样式(范围:最小[0]最大[100]):
render() {
const { values: { rangeValue } } = this.state;
return(
<div style={{ width: `${rangeValue}%` }}></div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.