代码之家  ›  专栏  ›  技术社区  ›  Vikram Mahishi

使用redux响应本机导航

  •  0
  • Vikram Mahishi  · 技术社区  · 7 年前

    我有一个收件箱组件,它从服务器获取所有通知并在视图中列出。代码看起来像,

    import React, { Component } from 'react'
    import {
      View,
      FlatList,
      ActivityIndicator,
      TouchableOpacity
    } from 'react-native'
    
    import {List, ListItem, SearchBar} from 'react-native-elements'
    import Header from '../common/Header'
    import { Container } from 'native-base'
    import PushNotifications from '../../fcm/notifications/PushNotifications'
    import NotificationDetails from './NotificationDetails';
    
    export const Navigator = new StackNavigator({
      NotificationList: { screen: NotificationList },
      NotificationDetail: { screen: NotificationDetail },
    },{
      initialRouteName: 'NotificationList',
    })
    
    class NotificationList extends Component {
      constructor(props) {
        super(props)
    
        this.state = {
          loading: false,
          data: [],
          page: 1,
          seed: 1,
          error: null,
          refreshing: false
        }
        this.loadNotificationDetails = this.loadNotificationDetails.bind(this)
      }
    
      componentDidMount() {
        const{dispatch,actions} = this.props
        dispatch(actions.getNotification())
      }
    
      handleRefresh = () => {
        this.setState(
          {
            page: 1,
            seed: this.state.seed + 1,
            refreshing: true
          },
          () => {
            const{dispatch,actions} = this.props
            dispatch(actions.getNotification())
          }
        )
      }
    
      handleLoadMore = () => {
        this.setState(
          {
            page: this.state.page + 1
          },
          () => {
            const{dispatch,actions} = this.props
            dispatch(actions.getNotification())
          }
        );
      }
      renderSeparator = () => {
        return (
          <View
            style={{
              height: 1,
              width: "86%",
              backgroundColor: "#CED0CE",
              marginLeft: "14%"
            }}
          />
        );
      };
    
      renderHeader = () => {
        return <SearchBar placeholder="Type Here..." lightTheme round />
      }
    
      renderFooter = () => {
        if (!this.state.loading) return null;
    
        return (
          <View
            style={{
              paddingVertical: 20,
              borderTopWidth: 1,
              borderColor: "#CED0CE"
            }}
          >
            <ActivityIndicator animating size="large" />
          </View>
        )
      }
    
      loadNotificationDetails = () => {
        this.props.navigation.navigate('NotificationDetails')
      }
    
      render() {
        return (
          <Container >
            <Header />
            <List containerStyle={{ marginTop: 0, borderTopWidth: 0, borderBottomWidth: 0 }}>
              <FlatList
                data={this.props.listNotification}
    
                renderItem={({ item }) => (
                  <TouchableOpacity
                    onPress={() => this.loadNotificationDetails()}>
                    <ListItem
                      roundAvatar
                      title={`${item.text}`}
                      subtitle={item.dateTime}
                      // avatar={{ uri: item.picture.thumbnail }}
                      containerStyle={{ borderBottomWidth: 0 }}
                    />
                  </TouchableOpacity>
                )}
                ItemSeparatorComponent={this.renderSeparator}
                ListHeaderComponent={this.renderHeader}
                ListFooterComponent={this.renderFooter}
                onRefresh={this.handleRefresh}
                refreshing={this.state.refreshing}
                onEndReached={this.handleLoadMore}
                onEndReachedThreshold={50}
              />
            </List>
            <PushNotifications />
          </Container>
        )
      }
    }
    
    export default NotificationList;
    

    现在我想实现的是点击任何一个列表项,我想加载完整的详细通知。 发生的事情是当我点击它似乎失去了导航对象。因此,它的抱怨找不到导航的属性。 道具只有来自redux商店的项目,我无法理解如何将导航道具放入已经有来自redux商店道具的组件中? 我怎样才能做到这一点?任何帮助都非常感谢。

    谢谢, 维克拉姆

    1 回复  |  直到 7 年前
        1
  •  0
  •   maxhungry    7 年前

    StackNavigator 是工厂函数而不是构造函数。你试过了吗

    const Navigator = StackNavigator({
      NotificationList: { screen: NotificationList },
      NotificationDetail: { screen: NotificationDetail },
    },{
      initialRouteName: 'NotificationList',
    })
    

    这有点混乱,但是在v2中,团队将api更改为 createStackNavigator 是的。

    推荐文章