0

From one array, I have it displayed as a list on one component(Box.js) and stored in a react-select in another component(Search.js). Both of them are a same level children belonging to a parent component(trial.js).

Ultimately, I want to display the object either by clicking from the list or changing/selecting from the react-select. I've lifted the event handlers to their parents and succeeded in displaying the selected object independently.

However, I can't seem to sync the onClick with the onChange. In detail, I want the click event handler to make selected list bold and change the displayed item in react-strap and vice versa. The lifting state and syncing event handler example given in the react homepage uses text input, which doesn't really help with what I am trying to do..

Parent) Trial.js:

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;

Child with list) Box.js:

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;

child with react-select) remote.js:

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's child to render the selected object:

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;
episodewon
  • 29
  • 2
  • 7

1 Answers1

1

I think there is one issue in your code:

from react-select documentation, when you are handling an onChange event handler, you will get the selected option out of it in the form of {value:'',label:''} pair that you have passed to the component through options array, so if your options array is like: [{value: 'sth', label:'label'}], when the onChange event is fired and one of the options get selected, the onChange event handler caches the {value: 'sth', label:'label'} object from the array and if you want to pass the data to the parent you should write onChange={data => this.props.handleChange(data.value) } so in this manner, your value is the real object that has the information like color, but you are just passing the raw object that is being handled like -> selected.color in your code while the selected object is the {value:{}, label:''} because you have passed just the e object and instead should be passed like e.value so it contains the color information.

Mehrnaz.sa
  • 386
  • 1
  • 7