React native:如何从map()生成的组件中单独控制每个onpress()

时间:2017-06-24 12:58:03

标签: javascript reactjs react-native

我是新来的本地人。 根据数组中的元素数量,我连续渲染3个下拉列表,多次。 因此,如果数组有3个元素,那么将有3行3个下拉列表。 但是当从单个下拉列表调用onpress时,它会影响所有3行的相应元素。 我怎样才能单独控制每一行。 很多解决方案都说使用 bind ,但我尝试的方式并没有影响任何事情。 click to see the rendered layout looks for 3 elements in the arrray

namespace FieldUserControl_test
{
    /// <summary>
    /// Interaction logic for ClothesController.xaml
    /// </summary>
    public partial class ClothesController : UserControl
    {
        public ClothesController()
        {
            InitializeComponent();
            GridRoot.DataContext = this;
            //or
            this.DataContext = this;
        }

        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }

        }

        public int Value
        {
            get { return (int)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(ClothesController), new PropertyMetadata(""));
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(ClothesController), new PropertyMetadata(0));
    }
}

1 个答案:

答案 0 :(得分:1)

您必须将唯一键传递给onSelect方法,然后您才能单独控制每一行。在下面的例子中,你可以看到有两个数组并且为了获取内部数组的数据,我必须传递外部数组的索引以及内部数组的索引。这也将使其独一无二。

var arr1 = [1,2,3]
var arr2 = ['a','b','c']


function looper(data1,data2){
  data1.map((item1,index1) => {
   data2.map((item2,index2) =>console.log("item1: " + item1 + " " + index1 + " item2: " + item2 + " " + index2));
  })
}
looper(arr2,arr1);
相关问题