Reactjs 将数组发送到Redux表单以在“选择”下拉列表中生成选项
设置安全问题是身份验证的一部分。服务器以数组的形式给出20个左右问题的列表。我可以获得要渲染的表单和选择框,但通过指定索引,一次只能获得一个选项 如果我试图发送整个数组,我会得到一个Reactjs 将数组发送到Redux表单以在“选择”下拉列表中生成选项,reactjs,redux,react-redux,redux-form,Reactjs,Redux,React Redux,Redux Form,设置安全问题是身份验证的一部分。服务器以数组的形式给出20个左右问题的列表。我可以获得要渲染的表单和选择框,但通过指定索引,一次只能获得一个选项 如果我试图发送整个数组,我会得到一个未定义的错误。尝试在`中执行for循环,以迭代每个索引,这产生了一个错误 我试图找出如何传递整个数组,以便它为数组中的每个条目生成一个选项 这就是我到目前为止所做的: // ./set_security_questions.js // This renders errors regarding the form
未定义的
错误。尝试在`中执行for循环,以迭代每个索引,这产生了一个错误
我试图找出如何传递整个数组,以便它为数组中的每个条目生成一个选项
这就是我到目前为止所做的:
// ./set_security_questions.js
// This renders errors regarding the form inputs
renderField(field) {
const {
label,
placeholder,
type,
name,
questions,
meta: {
touched,
error
}
} = field;
return (
<div className='form-group'>
<label>{label}</label>
<select className='form-control' name={name}>
<option value={questions}>{questions}
</option>}
</select>
<input
className='form-control'
type={type}
placeholder={placeholder}
{...field.input}
/>
<div className='text-danger'>
{touched ? error : ""}
</div>
</div>
);
}
// The main body to be rendered
render() {
if (this.props.permitRender) {
const { handleSubmit } = this.props;
return (
<div>
<h3>Set Security Questions</h3>
<p>Please select two security questions that will be easy for you to remember.</p>
<form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
{this.renderAlert()}
<Field
questions={this.props.questions.data.security_question}
label='Question 1'
placeholder='Answer 1'
name='a1'
type='text'
component={this.renderField}
/>
<Field
label='Question 2'
placeholder='Answer 2'
name='a2'
type='text'
component={this.renderField}
/>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
);
} else if (!this.props.permitRender) {
return (
<div> { this.renderAlert() } </div>
);
}
}
下面是我当前用于填充一组复选框的示例。复选框组件中没有任何特殊的逻辑,我只是使用自定义html进行样式设置
这是我的数据,一个简单的数组:
const env = {
FONT_FORMATS: ['OTF', 'TTF', 'WOFF', 'WOFF2']
}
下面是我的组件的render()函数中的代码。我将每个项目存储在Redux表单中的objectkey->fileTypes下
<ul className='tags'>
{envConfig.FONT_FORMATS.map((tag: string) => (
<Field
key={tag}
value={tag}
name={`fileTypes.[${tag}]`}
id={tag.toLowerCase().replace(' ', '_')}
type='checkbox'
component={checkBox}
label={tag}
/>
))}
</ul>
{envConfig.FONT_FORMATS.map((标记:string)=>(
))}
我希望这对你有帮助
<ul className='tags'>
{envConfig.FONT_FORMATS.map((tag: string) => (
<Field
key={tag}
value={tag}
name={`fileTypes.[${tag}]`}
id={tag.toLowerCase().replace(' ', '_')}
type='checkbox'
component={checkBox}
label={tag}
/>
))}
</ul>