繁体   English   中英

如何在 React Native 中将函数作为属性传递给组件

[英]How to pass a function to a component as a property in React Native

我创建了一个 JS 函数,它返回一个带有一些子组件的视图,我正在重用代码。我想知道如何将函数传递给由函数创建的组件

 const MenuItem = ({title,indicatorColor,index,onPressItem}) => ( <TouchableOpacity onPress={()=>onPressItem(index)} > <View style={{ paddingLeft: 20, paddingBottom: 15, paddingTop: 15, flexDirection: 'row', width: 150, borderRightWidth: 2, borderRightColor: 'Colors.GREY_TWO', backgroundColor: indicatorColor, alignItems: 'center', }}> <View style={{ backgroundColor: 'black', height: 5, width: 5, borderRadius: 3, alignSelf: 'center', }} /> <Text style={{fontSize: 15, left: 5,alignItems: 'center',}}>{title}</Text> </View> </TouchableOpacity> ); const onMenuItemPress = (index) => { console.log('menu selected:'.index); } <MenuItem title='Sort' indicatorColor='red' index='0' onPress={onMenuItemPress} />

上面的代码抛出一个错误,说 onMenuPress 不是一个函数,请提出建议。

const MenuItem = ({title,indicatorColor,index,onPressItem}) => (
          <TouchableOpacity onPress={()=>onPressItem(index)} >
            <View
              style={{
                paddingLeft: 20,
                paddingBottom: 15,
                paddingTop: 15,
                flexDirection: 'row',
                width: 150,
                borderRightWidth: 2,
                borderRightColor: 'Colors.GREY_TWO',
                backgroundColor: indicatorColor,
                alignItems: 'center',
              }}>
              <View 
                style={{
                  backgroundColor: 'black',
                  height: 5,
                  width: 5,
                  borderRadius: 3,
                  alignSelf: 'center',

                }} 
              /> 
              <Text style={{fontSize: 15, left: 5,alignItems: 'center',}}>{title}</Text>
            </View>
          </TouchableOpacity>
      );

      const onMenuItemPress = (index) => {
          console.log('menu selected:'.index);
      }

      <MenuItem title='Sort' indicatorColor='red' index='0' onPressItem={onMenuItemPress} />

您应该在道具中使用 onPressItem 而不是 onPress

将函数包装在您要返回的Component中:

const MenuItem = ({title,indicatorColor,index,onPressItem}) => {

     const onMenuItemPress = (index) => {
         console.log('menu selected:'.index);
     }

     return (
      <TouchableOpacity onPress={()=>onPressItem(index)} >
        <View
          style={{
            paddingLeft: 20,
            paddingBottom: 15,
            paddingTop: 15,
            flexDirection: 'row',
            width: 150,
            borderRightWidth: 2,
            borderRightColor: 'Colors.GREY_TWO',
            backgroundColor: indicatorColor,
            alignItems: 'center',
          }}>
          <View 
            style={{
              backgroundColor: 'black',
              height: 5,
              width: 5,
              borderRadius: 3,
              alignSelf: 'center',

            }} 
          /> 
          <Text style={{fontSize: 15, left: 5,alignItems: 'center',}}>{title} 
          </Text>
        </View>
      </TouchableOpacity>
     )
  };


  <MenuItem title='Sort' indicatorColor='red' index='0' onPress={onMenuItemPress} />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM