passing function from a parent container to a child container using swipeout in react-native

Refresh

December 2018

Views

18 time

2

I'm trying to pass a function from a parent container to a child container in react native. On the screen the user is presented with a list of items where the user can swipe the list to reveal more options

Child

    import React from 'react';
    import { StyleSheet, Text, View, Image} from 'react-native';
    import { Container, Content, Button, Icon, List, ListItem,Body,Left,Thumbnail } from 'native-base';
    import Swipeout from 'react-native-swipeout';

    const swipeBtns = [
      {
        component: (
          <View
              style={{
                flex: 1,
                alignItems: 'center',
                justifyContent: 'center',
                flexDirection: 'column',
              }}
          >
            <Image source={require('../../../assets/imgs/trash.png')} />
          </View>
        ),
        backgroundColor: '#f15151',
        onPress: () => {
          onDeleteGroup()

        }

    },
    {
      component: (
        <View
            style={{
              flex: 1,
              alignItems: 'center',
              justifyContent: 'center',
              flexDirection: 'column',
            }}
        >
          <Image source={require('../../../assets/imgs/edit.png')} />
        </View>
      ),
      backgroundColor: '#1b6faa',
      onPress: () => {
        console.log("Edit Item");
      }
    }
    ];

    const CreatedGroupsScreen = ({navigation, myGroups, onDeleteGroup}) => (

       <Container>
           <Content contentContainerStyle={{justifyContent:'center'}}>
           <Button 
           onPress={()=>navigation.navigate('CreateGroup')}
           style={{width:'90%', alignSelf:'center', margin:10}} block bordered iconRight>
               <Icon name="add"/>
               <Text style={{fontSize:15}}>
                Create New Group
               </Text>
           </Button>
                <List
                dataArray={myGroups}
                renderRow = {(item)=>{
                return (
                  <Swipeout right={swipeBtns} autoClose="true" backgroundColor= 'transparent'>

                <ListItem 
                onPress={()=>navigation.navigate('GroupPosts')}
                avatar>
                  <Left>
                  <Thumbnail source={require(`../../../assets/imgs/group.png`)} />
                  </Left>

                  <Body>
                    <Text style={{fontWeight:'700', fontSize:16, color:'#1b6faa'}}>{item.groupName}</Text>
                    <Text style={{fontSize:15, color:'#5f5f5f'}} >{item.about}</Text>
                  </Body>

                </ListItem>
                </Swipeout>
                )}
                }>
              </List>
           </Content>
       </Container>

    );
export default CreatedGroupsScreen;

Parent

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Alert} from 'react-native';
import CreatedGroupsScreen from './CreatedGroupsScreen';

class CreatedGroupsContainer extends Component{

  state = {
    myCreatedGroups: [
      { groupName: 'group1', about: 'bla bla bla' },
      { groupName: 'group2',   about: 'bla bla abla' },
      { groupName: 'group3', about: 'bla bla bla' },
    ],

  }

handleDeleteGroup = () => {
  Alert.alert(
    'Delete Group',
    'Are you sure to delete group?',
    [
      {text: 'No', onPress: () => console.log('Ask me later pressed')},
      {text: 'Yes, Delete', onPress: () => console.log('Cancel Pressed'), style: 'cancel'}
    ],
    { cancelable: false }
  )
}
  render() {
    return (
     <CreatedGroupsScreen
     navigation = {this.props.navigation}
     myGroups = {this.state.myCreatedGroups}
     onDeleteGroup = {this.handleDeleteGroup}
     />
    );
  }
}
export default CreatedGroupsContainer;

When i swipe on the list and tap on the delete icon, an error messages pops up the screen saying "Cant find variable onDeleteGroup". Any help on how to get it working?

1 answers

1

onDeleteGroupне может быть найдена причина это не в правильном объеме в компоненте ребенка. Таким образом, переместить конфигурацию ( const swipeBtns) для <Swipeout />компонента внутри < CreatedGroupsScreen />самого компонента, например:

const CreatedGroupsScreen = ({navigation, myGroups, onDeleteGroup}) => {
   const swipeBtns = [
     CONFIG_GOES_HERE
   ];

   return (
    <Container>
       <Content contentContainerStyle={{justifyContent:'center'}}>
       <Button 
       onPress={()=>navigation.navigate('CreateGroup')}
       style={{width:'90%', alignSelf:'center', margin:10}} block bordered iconRight>
           <Icon name="add"/>
           <Text style={{fontSize:15}}>
            Create New Group
           </Text>
       </Button>
            <List
            dataArray={myGroups}
            renderRow = {(item)=>{
            return (
              <Swipeout right={swipeBtns} autoClose="true" backgroundColor= 'transparent'>

            <ListItem 
            onPress={()=>navigation.navigate('GroupPosts')}
            avatar>
              <Left>
              <Thumbnail source={require(`../../../assets/imgs/group.png`)} />
              </Left>

              <Body>
                <Text style={{fontWeight:'700', fontSize:16, color:'#1b6faa'}}>{item.groupName}</Text>
                <Text style={{fontSize:15, color:'#5f5f5f'}} >{item.about}</Text>
              </Body>

            </ListItem>
            </Swipeout>
            )}
            }>
          </List>
       </Content>
   </Container>
 );

};