小编aha*_*ond的帖子

在从 API 调用填充的反应选择下拉菜单上设置默认值

class Select extends React.PureComponent {
    constructor(props) {
        super(props)
        this.state = { value: this.props.defaultValue }
        this.handleChange = this.handleChange.bind(this)
    }

    handleChange(e) {
        e.persist()
        if (typeof this.props.onDataChange !== 'undefined') {
            this.setState({ value: e.target.value }, () => this.props.onDataChange(e))
        } else {
            this.setState({ value: e.target.value })
        }
    }

    render() {
        const { options } = this.props
        return (
            <div>
                <select
                    value={this.state.value}
                    onChange={this.handleChange}
                >
                    {options.map((option, i) => {
                        const value = option.value || option.path || null
                        const label = option.label || option.name || option

                        return ( …
Run Code Online (Sandbox Code Playgroud)

javascript default render drop-down-menu reactjs

6
推荐指数
1
解决办法
4764
查看次数

标签 统计

default ×1

drop-down-menu ×1

javascript ×1

reactjs ×1

render ×1