Select 具有动态选项

Select with dynamic options

我正在尝试创建一个动态的 select,其中包含处于某个状态的选项。

我试过两件事:

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>
        )
    }
}

但是当我更新 templateList(使用输入和按钮)时,对象不会重新呈现新的 选项(我觉得这很奇怪)。我知道 addOption 函数正在运行,因为它通过了它的 reducer

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

例如,您没有提供 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