首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过单击React中的外部组件更改react-select的显示值

通过单击React中的外部组件更改react-select的显示值
EN

Stack Overflow用户
提问于 2018-09-07 04:14:19
回答 1查看 1.7K关注 0票数 0

在一个数组中,我将其显示为一个组件(Box.js)上的列表,并存储在另一个组件(Search.js)中的react-select中。它们都是属于父组件(trial.js)的同一级别的子项。

最终,我希望通过从列表中单击或从react-select中更改/选择来显示对象。我已经将事件处理程序提升到它们的父级,并成功地独立显示了所选对象。

但是,我似乎无法将onClick与onChange同步。具体来说,我希望click事件处理程序将选定列表设置为粗体,并更改react-strap中显示的项,反之亦然。react主页中给出的提升状态和同步事件处理程序示例使用文本输入,这对我正在尝试做的事情没有真正的帮助。

父级) Trial.js:

代码语言:javascript
复制
import React, { Component } from 'react';
import { colourOptions } from './data';
import { Grid, Row } from 'react-flexbox-grid';
import Box from './box';
import Remote from './remote';

    class Trial extends Component{
        constructor(props) {
            super(props);
            this.state = {
                selected:'',
            }
        }

        getValue(e){
            this.setState({
                selected: e
            })
        }


          render() {
              return ( 
                <Grid fluid className="full-height">
                    <Row className="full-height">

                    <Box 
                        colourOptions={colourOptions}
                        handleChange={this.getValue.bind(this)}
                    />

                    <Remote 
                        colourOptions={colourOptions}
                        selected={this.state.selected}
                        handleChange={this.getValue.bind(this)}
                    />
                    </Row>
                </Grid>
          ) 
        }
    }

    export default Trial;

带有列表的子项) Box.js:

代码语言:javascript
复制
import React, { Component } from 'react';
import { Col } from 'react-flexbox-grid';

class Box extends Component{
    constructor(props) {
        super(props);
        this.state = {
        }
    }

    clicked(e){
        this.props.handleChange(e)
    }

    render() {

          return ( 
                <Col md={9} className="col2">
                    <ul>
                        {this.props.colourOptions.map((r,i) => 
                            <li key={i} onClick={()=>this.clicked(r)}> {r.label} </li>
                            )}
                    </ul>
                </Col>


      ) 
    }
}

export default Box;

react-select的子项) remote.js:

代码语言:javascript
复制
import React, { Component } from 'react';
import Select from 'react-select';
import { Col } from 'react-flexbox-grid';
import RenderComp from './rendercomp';

class Remote extends Component{
    constructor(props) {
        super(props);
        this.state = {
        }
    }

    clicked(e){
        this.props.handleChange(e)
    }

      render() {
          return ( 

        <Col md={3} className="col1">

            <Select
                options={this.props.colourOptions}
                onChange={this.clicked.bind(this)}
            />                
            <RenderComp
                selected={this.props.selected}
            />
        </Col>


      ) 
    }
}

export default Remote;

用于呈现所选对象的remote.js的子级:

代码语言:javascript
复制
import React, { Component } from 'react';

class Remote extends Component{
    constructor(props) {
        super(props);
        this.state = {
        }
    }

    renderComp(selected){
        return(
            <ul>
                <li>
                {selected.label}
                </li>
                <li>
                {selected.color}
                </li>
            </ul>            
        )
    }

    render() {
        if(this.props.selected){
            return (
                <div>
                    {this.renderComp(this.props.selected)}
                </div>
                );
        }else{
            return(
                <div></div>
            )
        } 
    }
}

export default Remote;
EN

回答 1

Stack Overflow用户

发布于 2018-09-07 05:26:44

我认为你的代码中有一个问题:

从react-select文档中,当您处理onChange事件处理程序时,您将以您通过选项数组传递给组件的{value:'',label:''}对的形式从中获取所选选项,因此,如果您的选项数组如下:{value:'sth',label:'label'},当触发onChange事件并选择其中一个选项时,onChange事件处理程序将缓存{value:'sth',label:'label'}对象从数组中,如果你想将数据传递给父对象,你应该写onChange={data => this.props.handleChange(data.value) },所以在这种方式下,你的值是具有颜色等信息的真实对象,但你只是传递了原始对象,该对象在你的代码中被像-> selected.color一样处理,而所选对象是{ value :{},label:''},因为你只传递了e对象,而应该像e.value一样传递,以便它包含颜色信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52211536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档