简体   繁体   English

如何在React中为select元素自定义选项?

[英]How to customize options in for a select element in React?

I have a page in React called Anish that renders a Dropdown component: 我在React中有一个名为Anish的页面,该页面呈现了一个Dropdown组件:

import React from 'react';
import Dropdown from 'components/Dropdown';

class Anish extends React.Component {
  render() {
    return (
      <Dropdown />
    );
  }
}

export default Anish;

Here is the Dropdown component: 这是下拉列表组件:

import React from 'react';
import '../../styles/dropdown.scss';

const options = [
  'Select an Option',
  'First Option',
  'Second Option',
  'Third Option',
];

export default class Dropdown extends React.Component {
  constructor(props) {
    super(props);

    this.state = { value: 'Select an Option' };
    this.onChange = this.onChange.bind(this);
  }

  onChange(e) {
    this.setState({
      value: e.target.value,
    });
  }

  render() {
    const dropdownOptions = options.map((option) =>
      <option value={option} key={option} >{option}</option>,
    );

    return (
      <select
        value={this.state.value}
        onChange={this.onChange}
        styleName="dropdown"
      >
        { dropdownOptions }
      </select>
    );
  }
}

So how am I able to customize the options for the <Dropdown /> in other pages? 那么,如何在其他页面中为<Dropdown />自定义选项? Can I do something like <Dropdown options={['Volvo', 'Subaru']} /> ? 我可以做类似<Dropdown options={['Volvo', 'Subaru']} />事情吗?

I think I'm supposed to define props somewhere, but I am new to all this. 我想我应该在某个地方定义道具,但是我对这一切还是陌生的。 Thank you for the help, 感谢您的帮助,

Yes you can pass the options in as a prop and do this.props.options inside of the child component or if you want to customize the way the options look, you can pass in an array of elements that you can map out in the Dropdown class. 是的,您可以将选项作为prop传入,并在子组件内部执行this.props.options;如果要自定义选项的外观,则可以传入一系列元素,这些元素可以在Dropdown中映射出来类。

You would change: 您将更改:

 const dropdownOptions = this.props.options.map((option) => <option value={option} key={option} >{option}</option>, ); 

Any items you pass as attributes to your JSX components will get passed as part of the props object in the component ( props is present on the context of the component, ie this ): 您作为属性传递给JSX组件的所有项目都将作为组件中props对象的一部分传递( props存在于组件的上下文中,即this ):

<Dropdown options={['Volvo', 'Subaru']} />
//        ^-- this means props objects will look like { options: ['Volvo', 'Subaru'] }

Which means you should read the options from this.props : 这意味着您应该阅读this.propsoptions

export default class Dropdown extends React.Component {

  // ...

  render() {

  // use this.props.options
  const dropdownOptions = this.props.options.map((option) =>
    <option value={option} key={option} >{option}</option>,
  );

  return (
    <select
      value={this.state.value}
      onChange={this.onChange}
      styleName="dropdown"
    >
      { dropdownOptions }
    </select>
  );
}

As a side not, if you use a plain function ( presentational component ) rather than a class, props are passed as the first argument to the function: 顺便说一句,如果您使用普通函数( 表示组件 )而不是类,则将props作为函数的第一个参数传递:

export default function Dropdown(props) {
  //                             ^ props passed here

  // use props.options
  const dropdownOptions = props.options.map((option) =>
    <option value={option} key={option} >{option}</option>,
  );

  return (
    <select
      value={this.state.value}
      onChange={this.onChange}
      styleName="dropdown"
    >
      { dropdownOptions }
    </select>
  );
}

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

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