简体   繁体   English

使用动态生成的选项为 Select 响应 defaultValue

[英]React defaultValue for Select with Dynamically Generated Options

Use the defaultValue or value props on instead of setting selected on .使用 defaultValue 或 value 道具而不是设置 selected on 。

<select defaultValue="react">
 <option value="react">React</option>
 <option value="angular">Angular</option>
</select>

defaultValue would work with the above select tag. defaultValue 将与上述选择标签一起使用。 However, it does not seem to work with options generated by loop.但是,它似乎不适用于循环生成的选项。

<select defaultValue={selectedOptionId}>
{option_id.map(id =>
  <option key={id} value={id}>{options[id].name}</option>
)}
</select>

Probably options not fully been set when defaultValue was declared?可能在声明 defaultValue 时没有完全设置选项?

I could do manual assigning in componentDidUpdate() and onChange event.我可以在componentDidUpdate()onChange事件中进行手动分配。

But my question is - Is there any cleaner(better) way to solve it?但我的问题是 - 有没有更清洁(更好)的方法来解决它?

Thanks.谢谢。

This is old, but since answer #1 is related to a controlled Select and the question seems to be related to uncontrolled Select I think is worth to leave some lines for future readers:这是旧的,但由于答案 #1 与受控Select相关,而问题似乎与不受控制的Select相关,我认为值得为未来的读者留下一些行:

The problem is that for uncontrolled components React needs to know what are the options before the first render , since from that moment the defaultValue won't override the current value of the Select .问题在于,对于uncontrolled组件,React 需要在第一次渲染之前知道有哪些options ,因为从那时起defaultValue不会覆盖Select的当前值。 This means that if you render the Select before the options it won't know what to select.这意味着如果您在options之前渲染Select ,它将不知道要选择什么。

You can solve the problem avoiding the render before the options are available:您可以在options可用之前解决避免render的问题:

const RenderConditionally = ({ options, selected }) => options.length > 0 ? (
  <select defaultValue={selected}>
   {options.map(item => (
     <option key={item.id} value={item.value}>{item.label}</option>
   ))}
  </select>
) : null;

Or without ternary if you desire:如果您愿意,也可以不使用三元:

const RenderConditionally = ({ options, selected }) => {
  if (options.length === 0) {
    return null;
  }

  return (
    <select defaultValue={selected}>
      {options.map(item => (
        <option key={item.id} value={item.value}>{item.label}</option>
      ))}
    </select>
  );
};

For users running into this issue, you can get the desired functionality by using the value prop, instead of defaultValue , eg:对于遇到此问题的用户,您可以通过使用value defaultValue而不是defaultValue来获得所需的功能,例如:

<select value={selectedOptionId}>
  {option_id.map(id =>
    <option key={id} value={id}>{options[id].name}</option>
  )}
</select>

Most probably you have something wrong with option_id and options arrays structure, or selectedOptionId variable.很可能您对option_idoptions数组结构或selectedOptionId变量有问题。 The way you build your select component is ok.您构建选择组件的方式是可以的。

I've made a fiddle where this code works fine:我做了一个小提琴,这段代码可以正常工作:

render: function() {
let option_id = [0, 1];
let options = [{name: 'a'}, {name: 'b'}];
let selectedOptionId = 1
return (
  <select defaultValue={selectedOptionId}>
    {option_id.map(id =>
    <option key={id} value={id}>{options[id].name}</option>
    )}
  </select>
)

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM