首頁  >  問答  >  主體

ReactJS中如何將資料從子元件傳遞到父元件?

<p>我正在嘗試將資料從子元件傳送到其父元件,如下所示:</p> <pre class="brush:php;toolbar:false;">const ParentComponent = React.createClass({ getInitialState() { return { language: '', }; }, handleLanguageCode: function(langValue) { this.setState({language: langValue}); }, render() { return ( <div className="col-sm-9" > <SelectLanguage onSelectLanguage={this.handleLanguage}/> </div> ); });</pre> <p>這是子組件:</p> <pre class="brush:php;toolbar:false;">export const SelectLanguage = React.createClass({ getInitialState: function(){ return{ selectedCode: '', selectedLanguage: '', }; }, handleLangChange: function (e) { var lang = this.state.selectedLanguage; var code = this.state.selectedCode; this.props.onSelectLanguage({selectedLanguage: lang}); this.props.onSelectLanguage({selectedCode: code}); }, render() { var json = require("json!../languages.json"); var jsonArray = json.languages; return ( <div > <DropdownList ref='dropdown' data={jsonArray} value={this.state.selectedLanguage} caseSensitive={false} minLength={3} filter='contains' onChange={this.handleLangChange} /> </div> ); } });</pre> <p>我需要的是在父元件中取得使用者選擇的值。我收到此錯誤:</p> <pre class="brush:php;toolbar:false;">Uncaught TypeError: this.props.onSelectLanguage is not a function</pre> <p>誰能幫我找出問題所在嗎? </p> <p>附註子元件正在從 json 檔案建立一個下拉列表,我需要下拉列表來顯示彼此相鄰的 json 數組的兩個元素(例如:「aaa,english」作為首選!)</p> <pre class="brush:php;toolbar:false;">{ "languages":[ [ "aaa", "english" ], [ "aab", "swedish" ], }</pre> <p><br /></p>
P粉441076405P粉441076405441 天前428

全部回覆(2)我來回復

  • P粉652495194

    P粉6524951942023-08-28 09:50:36

    將資料從子元件傳遞到父元件

    在父元件中:

    getData(val){
        // do not forget to bind getData in constructor
        console.log(val);
    }
    render(){
     return(<Child sendData={this.getData}/>);
    }

    在子元件中:

    demoMethod(){
       this.props.sendData(value);
     }

    回覆
    0
  • P粉946437474

    P粉9464374742023-08-28 00:20:33

    這應該有效。在發送回 prop 時,您將其作為物件發送,而不是將其作為值發送,或將其用作父組件中的物件。其次,您需要格式化 json 物件以包含名稱值對,並使用 DropdownList

    valueFieldtextField 屬性

    簡答

    #

    父級:

    #
    <div className="col-sm-9">
         <SelectLanguage onSelectLanguage={this.handleLanguage} /> 
    </div>

    孩子:

    handleLangChange = () => {
        var lang = this.dropdown.value;
        this.props.onSelectLanguage(lang);            
    }

    詳細:

    編輯:

    考慮到 React.createClass 從 v16.0 開始已被棄用,最好透過擴充 React.Component 來建立 React 元件。使用此語法將資料從子元件傳遞到父元件將如下所示

    父級

    class ParentComponent extends React.Component {
    
        state = { language: '' }
    
        handleLanguage = (langValue) => {
            this.setState({language: langValue});
        }
    
        render() {
             return (
                    <div className="col-sm-9">
                        <SelectLanguage onSelectLanguage={this.handleLanguage} /> 
                    </div>
            )
         }
    }

    孩子

    var json = require("json!../languages.json");
    var jsonArray = json.languages;
    
    export class SelectLanguage extends React.Component {
        state = {
                selectedCode: '',
                selectedLanguage: jsonArray[0],
            }
    
        handleLangChange = () => {
            var lang = this.dropdown.value;
            this.props.onSelectLanguage(lang);            
        }
    
        render() {
            return (
                <div>
                    <DropdownList ref={(ref) => this.dropdown = ref}
                        data={jsonArray} 
                        valueField='lang' textField='lang'
                        caseSensitive={false} 
                        minLength={3}
                        filter='contains'
                        onChange={this.handleLangChange} />
                </div>            
            );
        }
    }

    使用OP在其答案中使用的createClass語法 父級

    const ParentComponent = React.createClass({
        getInitialState() {
            return {
                language: '',
            };
        },
    
        handleLanguage: function(langValue) {
            this.setState({language: langValue});
        },
    
        render() {
             return (
                    <div className="col-sm-9">
                        <SelectLanguage onSelectLanguage={this.handleLanguage} /> 
                    </div>
            );
    });

    孩子

    var json = require("json!../languages.json");
    var jsonArray = json.languages;
    
    export const SelectLanguage = React.createClass({
        getInitialState: function() {
            return {
                selectedCode: '',
                selectedLanguage: jsonArray[0],
            };
        },
    
        handleLangChange: function () {
            var lang = this.refs.dropdown.value;
            this.props.onSelectLanguage(lang);            
        },
    
        render() {
    
            return (
                <div>
                    <DropdownList ref='dropdown'
                        data={jsonArray} 
                        valueField='lang' textField='lang'
                        caseSensitive={false} 
                        minLength={3}
                        filter='contains'
                        onChange={this.handleLangChange} />
                </div>            
            );
        }
    });

    JSON:

    #
    { 
    "languages":[ 
    
        { 
        "code": "aaa", 
        "lang": "english" 
        }, 
        { 
        "code": "aab", 
        "lang": "Swedish" 
        }, 
      ] 
    }

    回覆
    0
  • 取消回覆