0

1 つの配列から、1 つのコンポーネント (Box.js) にリストとして表示し、別のコンポーネント (Search.js) の反応選択に格納します。どちらも親コンポーネント(trial.js)に属する同レベルの子です。

最終的に、リストからクリックするか、反応選択から変更/選択して、オブジェクトを表示したいと思います。イベント ハンドラーを親に持ち上げて、選択したオブジェクトを個別に表示することに成功しました。

ただし、onClick を onChange と同期できないようです。具体的には、クリック イベント ハンドラーで、選択したリストを太字にして、react-strap で表示されるアイテムを変更したり、その逆をしたりします。反応ホームページに示されているリフティング状態と同期イベント ハンドラーの例では、テキスト入力を使用していますが、これは私がやろうとしていることにはあまり役に立ちません..

親) 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;

リストを持つ子)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;

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 の子:

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;
4

1 に答える 1

1

あなたのコードには1つの問題があると思います:

react-select ドキュメントから、onChange イベント ハンドラーを処理しているときに、オプション配列を介してコンポーネントに渡した {value:'',label:''} ペアの形式で、選択したオプションを取得します。したがって、オプション配列が [{value: 'sth', label:'label'}] のような場合、onChange イベントが発生し、オプションの 1 つが選択されると、onChange イベント ハンドラーは {value: 'sth' をキャッシュします', label:'label'} オブジェクトを配列から取得し、データを親に渡したい場合は onChange={data => this.props.handleChange(data.value) } と記述する必要があるため、この方法で値は色などの情報を持つ実際のオブジェクトですが、コードで -> selected.color のように処理される生のオブジェクトを渡すだけで、選択されたオブジェクトは {value:{} です。label:''} は、e オブジェクトだけを渡したので、代わりに e.value のように渡す必要があるため、色情報が含まれているためです。

于 2018-09-06T21:26:44.253 に答える