redux-form 获取输入的表单名称
redux-form get the name of form for the input
在字段组件中,有没有办法获取表单的名称?
我对不同的表单使用相同的输入组件,实际上我使用硬编码表单名称,但我想找到一种方法来获取包含输入的表单名称
class FormField extends Component {
process = () => {
nameForm= 'myForm'; // hardcode, i'd like to get the form parent
const values = this.props.myState.form[nameForm].values;
(...)
}
render()
<input
{...myInput}
type={typeInput}
autoComplete="off"
processInput={this.process()}
/>
}
class Form extends Component {
render() {
return (
<form onSubmit={handleSubmit}>
<div className="appBodyTitleSeparator" />
<div className="formSection">
<Field
name="customer_id"
component={FormField}
myState={this.props.myState}
/>
(...)
}
我的表单在另一个组件中分离,我不想使用道具来获取名称。
在您的表单中,您可以访问表单组件中的 formName
,例如 props.form
,您可以将其作为道具传递给 Field
组件。
class FormField extends Component {
process = () => {
const nameForm = this.props.formName
const values = this.props.myState.form[nameForm].values;
(...)
}
render()
<input
{...myInput}
type={typeInput}
autoComplete="off"
processInput={this.process()}
/>
}
class Form extends Component {
render() {
const {form} = this.props;
return (
<form onSubmit={handleSubmit}>
<div className="appBodyTitleSeparator" />
<div className="formSection">
<Field
name="customer_id"
formName={form}
component={FormField}
myState={this.props.myState}
/>
(...)
}
看到这个example
在字段组件中,有没有办法获取表单的名称? 我对不同的表单使用相同的输入组件,实际上我使用硬编码表单名称,但我想找到一种方法来获取包含输入的表单名称
class FormField extends Component {
process = () => {
nameForm= 'myForm'; // hardcode, i'd like to get the form parent
const values = this.props.myState.form[nameForm].values;
(...)
}
render()
<input
{...myInput}
type={typeInput}
autoComplete="off"
processInput={this.process()}
/>
}
class Form extends Component {
render() {
return (
<form onSubmit={handleSubmit}>
<div className="appBodyTitleSeparator" />
<div className="formSection">
<Field
name="customer_id"
component={FormField}
myState={this.props.myState}
/>
(...)
}
我的表单在另一个组件中分离,我不想使用道具来获取名称。
在您的表单中,您可以访问表单组件中的 formName
,例如 props.form
,您可以将其作为道具传递给 Field
组件。
class FormField extends Component {
process = () => {
const nameForm = this.props.formName
const values = this.props.myState.form[nameForm].values;
(...)
}
render()
<input
{...myInput}
type={typeInput}
autoComplete="off"
processInput={this.process()}
/>
}
class Form extends Component {
render() {
const {form} = this.props;
return (
<form onSubmit={handleSubmit}>
<div className="appBodyTitleSeparator" />
<div className="formSection">
<Field
name="customer_id"
formName={form}
component={FormField}
myState={this.props.myState}
/>
(...)
}
看到这个example