Как передать функцию компоненту как свойство в React Native - PullRequest
0 голосов
/ 13 января 2020

Я создал функцию 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 не является функцией, пожалуйста, предложите.

Ответы [ 2 ]

1 голос
/ 14 января 2020
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 в реквизите

0 голосов
/ 14 января 2020

Оберните функцию внутри 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} />
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...