如何从react-native listview项打开其他屏幕

问题描述 投票:1回答:3

当我在onPress的任何项目上做listview时,我正试图打开另一个屏幕。

<TouchableHighlight underlayColor={AppColors.black}
                            onPress={Actions.SubCategoryList(item.guid)}>
        <View>
                 <Item style={{flexDirection: 'row', height: 50, borderBottomWidth: borderWidth}}>
                    <Text style={{
                        fontFamily: AppStyles.fontFamily,
                        fontSize: 17,
                        flex: 5,
                        color: AppColors.black
                    }}>{item.category_name}</Text>
                    <Item style={{flex: 1, borderBottomWidth: 0, flexDirection: 'row', justifyContent: 'flex-end'}}>
                        <Text style={{
                            color: AppColors.grey,
                            fontFamily: AppStyles.fontFamily,
                            fontSize: 17,
                            marginRight: 15
                        }}>{item.active_tournaments}</Text>
                        <Image resizeMode="contain" source={require('../../assets/images/right.png')}
                               style={{width: 15, height: 15, marginTop: 3}}/>
                    </Item>
                </Item>
            </View>
        </TouchableHighlight>

但每当我进入当前屏幕时,它直接进入子类别屏幕而不点击。

我想知道如何从另一个屏幕上的当前屏幕捕获数据。

react-native react-native-ios react-native-router-flux react-native-listview
3个回答
1
投票

问题是你实际上是立即调用onPress而不是将其设置为回调。您可以在以下代码中看到:

onPress={Actions.SubCategoryList(item.guid)}

您有两种方法可以解决此问题。你的第一个选择是在那里添加一个函数调用,如下所示:

onPress={() => Actions.SubCategoryList(item.guid)}

你的第二个选择是更改Actions.SubCategoryList函数以返回如下所示的回调:

export function SubCategoryList(guid){
  return () => { // this gets called by onPress

    /* contents of function go here */

  }
}

理想情况下,您还可以保留基于guid创建的回调的缓存,并返回缓存副本而不是创建新副本。这称为memoization,看起来像这样:

let cache = {};
export function SubCategoryList(guid){
  return cache[guid] || (cache[guid] = () => { 

    /* contents of function go here */

  })
}

0
投票

我已经尝试使用列表视图点击登录,请参阅示例代码

constructor() {
    super();
    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.state = {
      dataSource: ds.cloneWithRows(['row 1', 'row 2']),
    };
  }    
<View>
        <ListView
        dataSource={this.state.dataSource}
        renderRow={(rowData) => 
      <TouchableHighlight onPress={()=>{console.log("clicked-- data-->>",rowData)}}>
      <Text>{rowData}</Text>
        </TouchableHighlight>}
      />
        </View>

0
投票

您可以轻松使用StackNavigator,它允许您浏览屏幕,传递参数,即:您可以使用的列表项,如下所示:

class HomeScreen extends React.Component {
    static navigationOptions = {
    title: 'Welcome',
    };
    render() {
        const { navigate } = this.props.navigation;
        return (
        <Button
          title="Go to Jane's profile"
          onPress={() =>
          navigate('Profile', { name: 'Jane' }) // Sth. you want to pass to next view/screen
           }
        />
    );
    }
}

然后在所需的屏幕中,您可以从道具中获得您想要的项目:ex。

initialRoute={{
   component: MyScene,
   title: 'My Initial Scene',
   passProps: {myProp: 'foo'},
}}

另一个很好的参考:React Navigation ComdeMentor

© www.soinside.com 2019 - 2024. All rights reserved.