将数组从屏幕传递到组件

时间:2018-10-09 10:03:23

标签: reactjs react-native

我无法从Screen(App.js)中传递数组(或变量)以填充组件中的另一个数组。

app.js:

<SelectBoxPopUp values={brands}>
</SelectBoxPopUp>

component.js:

import React from 'react';
import {StyleSheet, View, Text, TouchableOpacity} from 'react-native';

let array1 = this.props.values,
    array2 = this.props.values,
    radioProps = [];
for ( var i = 0; i < array1.length; i++ ) {
  radioProps.push({
      label: array1[i],
      value: array2[i]
  });
}

export default class SelectBoxPopup extends React.Component {

  constructor(props) {
    super(props);
    this.state = {value: 0};
  }

  render() {
    return (
    <View>
    <Text>...</Text>
    </View>
    )
  }
}

1 个答案:

答案 0 :(得分:0)

您正在做的事情在SelectBoxPopup组件中是错误的。您应该执行以下操作

   export default class SelectBoxPopup extends React.Component {

     constructor(props) {
        super(props);
        this.state = {value: 0, radioProps: []};
     }

    componentDidMount(){
         let array1 = this.props.values,
              array2 = this.props.values,
              radioProps = [];
        for ( var i = 0; i < array1.length; i++ ) {
             radioProps.push({
                  label: array1[i],
                  value: array2[i]
             });
         }
         this.setState({
              radioProps
         });
        }

     render() {
         return (
           <View>
               <Text>...</Text>
          </View>
      )
    }
   }