【问题标题】:Clickable Flat list item with Navigate something page in React Native在 React Native 中带有导航页面的可单击平面列表项
【发布时间】:2021-01-07 08:35:48
【问题描述】:

我把本地图片和按钮放在平面列表中。

  1. 如何从 Json 中检索图像?

  2. 当我点击时如何移动到另一个页面?

我想分割脚本。

这是我的代码

export default class HotFlatList extends Component {
    render() {
    return(
        <View style={{flex:1, flexDirection:'column'}}>
            <View>
                <FlatList style={{backgroundColor:'block', opacity: 1}}
                    horizontal={true}
                    data={hotFlatListData}
                    renderItem={({item, index}) => {
                        return (
                            // <HorizontalFlatListItem item={item} index={index} parentFlatList={this}>
                            // </HorizontalFlatListItem>
                            <TouchableOpacity 
                            onPress={() => navigation.navigate("../components/Hook")}
                            style={{marginLeft: 20, alignItems: 'center'}}>
                                <Image source={require('../assets/apple.jpeg')} style={{borderRadius:30}}/>
                                <View style={{width: '100%', backgroundColor: '#02ad94', opacity: 0.5}}></View>
                                <Text style={{color: 'white', fontWeight: 'bold', fontSize:20, marginEnd: 5, marginTop: 5}}>apple</Text>
                            </TouchableOpacity>
                        );
                    }}
                    keyExtractor={item => item.hour}
                >

                </FlatList>
            </View>
        </View>
    );
            }
}

【问题讨论】:

  • 你是用react-navigation还是别的什么?

标签: react-native button navigation react-native-flatlist touchableopacity


【解决方案1】:

从 JSON 中检索图像,例如

const hotFlatListData = [{image: require('../assets/apple.jpeg')},{..}]

<Image source={item.image} style={{borderRadius:30, width:xx, height:xx}}/>

点击时移动另一个页面,比如

// remove this and extract "navigation" from function "props"
<TouchableOpacity onPress={() => navigation.navigate("ScreenRoute")}>
  ....
</TouchableOpacity>

【讨论】:

  • 感谢您的回答。图像代码工作得很好。 !如何转到另一个屏幕脚本?执行第二个代码时发生错误。脚本的名称是“Hook”。
  • @이원석 我已经更新了我的答案,因为你可以从函数中破坏navigation
  • 再次感谢您的回答。但它不适用于我的代码。从 '../components/Hook' 导入 Hook 后;我运行了下面的两个代码。 onPress={() => navigation.navigate("../components/Hook") onPress={() => navigation.navigate('Hook') 出现“找不到变量:导航”错误...
  • @이원석 请分享您的完整功能/代码
  • @이원석 在您的类组件中,这应该可以工作 this.props.navigation.navigate("Hook") 其中 Hook 不是路由的路径,它的路由实际上在您的堆栈导航器中定义为 name
猜你喜欢
  • 2020-05-04
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-13
  • 2021-04-27
相关资源
最近更新 更多