【问题标题】:Select with dynamic options使用动态选项进行选择
【发布时间】:2016-09-30 17:33:18
【问题描述】:

我正在尝试使用处于状态的选项创建动态选择。

我尝试了两件事:

  • 我尝试使用无状态函数创建自定义选择器。我将具有可以用选项填充选择的数据的道具传递给函数。像这样:

const renderFieldSelect = ({ name, label, templateList})  => {
	if(templateList && templateList.entities) {
		return (
			<div>
				<div className="col-md-4" >
					<label>{label}</label>
				</div>
				<div className="col-md-4">
					<Field
						className="form-control"
						name={name}
						component="select"
                    >
						{
							templateList.result.map((type, index) => 
								return <option value={templateList.entities.template[type].id}>{templateList.entities.template[type].name}</option>
							)
						}
					</Field>
				</div>
			</div>
		)
	}else {
		return <div></div>
}

//... more stuff

class Bla extends Component {
  
  //... more stuff
    render(){
        return (
            <div>
                <Field
                    component={renderFieldSelect}
                    name='templateList'
                    label='label'
                    templateList={this.props.templateList}
                />
                <Field
                    component="input"
                    name='addNewOption'
                    type='text'
                />
                <button onClick={addOption}>Add new option</button>
            </div>
        )
    }
}

但是当我更新模板列表(使用输入和按钮)时,对象不会用新的 选项(我觉得很奇怪)。我知道 addOption 函数正在工作,因为它通过它的 reducer

  • 我试图用 FieldArray 创建类似的东西,但我不知道如何更新 fields 参数。文档不太清楚这个道具有/可能有什么。另外,我也不知道你是否可以创建一个“字段”并只返回 the 或类似的东西。

【问题讨论】:

    标签: reactjs redux redux-form


    【解决方案1】:

    根据您提供的信息,很难确定您的问题。

    比如你没有提供reducer或者addOption函数。

    在任何情况下,我都做了一个简单的例子来证明你的这部分代码应该工作。我不能代表 redux 部分,因为你没有提供,我今天也不会编码。

    也许这至少会为您提供一些证据,证明问题出在代码的其他区域。

    import React, {Component} from 'react'
    import {Field, reduxForm, formValueSelector} from 'redux-form'
    import {connect} from 'react-redux'
    
    const renderFieldSelect = ({ name, label, templateList }) => {
      if (templateList && templateList.entities) {
        return (
          <div>
            <div className="col-md-4">
              <label>{label}</label>
            </div>
            <div className="col-md-4">
              <Field
                className="form-control"
                name={name}
                component="select">
                {
                  templateList.result.map((type, index) => {
                    return (<option value={templateList.entities.template[ type ].id}>{templateList.entities.template[ type ].name}</option>)
                  })
                }
              </Field>
            </div>
          </div>
        )
      } else {
        return <div></div>
      }
    
    }
    
    class Bla extends Component {
    
      constructor(props) {
        super(props)
        this.state = {}
        this.state.templateList = {}
        this.state.templateList.result = ['type1', 'type2']
        this.state.templateList.entities = {}
        this.state.templateList.entities.template = {
          type1: {id: 1, name: 'Type 1'},
          type2: {id: 2, name: 'Type 2'}
        }
      }
    
      render () {
        return (
          <div>
            <Field
              component={renderFieldSelect}
              name='templateList'
              label='label'
              templateList={this.state.templateList}
            />
            <Field
              component="input"
              name='addNewOption'
              type='text'
            />
            <button onClick={this.addOption.bind(this)}>Add new option</button>
          </div>
        )
      }
    
      addOption() {
        let type = {id: this.props.addNewOption, name: this.props.addNewOption}
        this.setState({
          ...this.state,
          templateList: {
            result: [...this.state.templateList.result, type.id],
            entities: {...this.state.templateList.entities, template: {...this.state.templateList.entities.template, [type.id]: type}}
          }
        })
      }
    }
    
    let bla = reduxForm({
      form: Bla.name
    })(Bla)
    
    const selector = formValueSelector(Bla.name)
    
    bla = connect(
      state => ({
        addNewOption: selector(state, 'addNewOption')
      })
    )(bla)
    
    export default bla

    【讨论】:

    • 问题实际上是减速器。感谢您的帮助
    猜你喜欢
    • 2017-12-20
    • 1970-01-01
    • 2014-04-14
    • 2019-01-19
    • 2018-09-26
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多