【问题标题】:passing function from a parent container to a child container using swipeout in react-native在 react-native 中使用 swipeout 将函数从父容器传递到子容器
【发布时间】:2018-11-30 14:27:23
【问题描述】:

我正在尝试将一个函数从父容器传递到反应本机的子容器。在屏幕上,用户会看到一个项目列表,用户可以在其中滑动列表以显示更多选项

孩子

    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;

父母

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;

当我在列表上滑动并点击删除图标时,屏幕上会弹出一条错误消息,提示“无法找到 onDeleteGroup 变量”。有关如何使其工作的任何帮助?

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    onDeleteGroup 找不到,因为它不在子组件的正确范围内。因此,将&lt;Swipeout /&gt; 组件的配置(const swipeBtns)移动到&lt; CreatedGroupsScreen /&gt; 组件本身内部,例如:

    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>
     );
    
    };
    

    【讨论】:

    • @developer 抛出了什么错误?和你的问题一样吗?你能提供那个具体的错误吗?
    • 一个常量不能在一个常量中,它会在第二个红色下划线
    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2016-12-25
    • 2022-12-15
    • 2020-04-01
    相关资源
    最近更新 更多