从平面列表项导航到另一个屏幕

Navigate to another screen from Flat list item

当我单击平面列表中的一项时,我试图导航到另一个屏幕。

我在这里的代码工作了几天,但现在它没有,当应用程序加载时,EventDetailScreen 甚至在我单击任何平面列表项之前就打开了,然后当我按 EventDetailScreen 的后退按钮,返回到 EventListScreen,如果我单击任何列表项,则没有任何反应,我也不会被带到 EventDetailScreen。

我也得到一个错误:

Warning: Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount.

我是 React Native 的新手,非常感谢任何帮助!

我正在使用: "react-navigation": "^2.7.0","react": "16.4.1", "react-native": "0.56.0",

我在 SO 上使用了这个答案 来让它开始工作。

EventListScreen.js

export default class EventListScreen extends Component {

constructor() {
    super();
    this.ref = firebase.firestore();
    this.unsubsribe = null;

    this.state = {
        eventName: '',
        eventLocation: '',
        loading: true,
        events: [],
    };
}

componentDidMount() {
    console.log('EventsListScreen1');
    this.unsubsribe = this.ref.onSnapshot(this.onCollectionUpdate)
}

componentWillUnmount() {
    this.unsubsribe();
}


openDetails = () => {
    this.props.navigation.navigate('EventDetailScreen');
};

render() {

    if (this.state.loading) {
        return null;
    }

    return (
        <Container>

            <FlatList
                data={this.state.events}
                // Get the item data by referencing as a new function to it
                renderItem={({item}) =>
                    <Event
                    openDetails={() => this.openDetails()}
                    {...item} />}
            />

            <View style={{flex: 1}}>
                <Fab
                    active={this.state.active}
                    direction="left"
                    containerStyle={{}}
                    style={{backgroundColor: '#5067FF'}}
                    position="bottomRight"
                    onPress={() => 
                    this.props.navigation.navigate('EventForm')
                    }>
                    <Icon 
                       name="ios-add"/>
                </Fab>
            </View>

        </Container>
    );
}

Event.js

export default class Event extends Component {

render() {

    return (
        <Card>
            <CardSection>
                <Text>{this.props.eventName}</Text>
            </CardSection>

            <TouchableOpacity
              onPress={this.props.openDetails()}
            >
            <CardSection>
                <Image
                    style={{
                       width: 350,
                       height: 300
                    }}
                    source={{
                       uri: this.props.imageDownloadUrl
                    }}
                />
            </CardSection>

            <CardSection>
                <Text>{this.props.eventLocation}</Text>
            </CardSection>
            </TouchableOpacity>
        </Card>
    );
}};

EventDetailScreen.js

export default class EventDetailScreen extends Component {
render() {
    /* 2. Get the param, provide a fallback value if not available */
    const { navigation } = this.props;
    const itemId = navigation.getParam('itemId', 'NO-ID');

    return (
        <View 
           style={{ 
              flex: 1,
              alignItems: 'center',
              justifyContent: 'center' 
            }}>
            <Text>Details Screen</Text>
        </View>
    );
}}

这可能是因为 Event 组件中的以下行。

<TouchableOpacity
    onPress={this.props.openDetails()} // <-- this line to be specific
 > ... </>

一旦EventScreenList渲染列表,第一行执行openDetails()切换屏幕的方法。

您可以使用 onPress={() => this.props.openDetails()} 来避免这种情况。

在 EventScreenList 组件的构造函数或 componentDidMount 中包含以下内容也是一个好主意,因为这两个函数都使用语句的 this 上下文。

this.openDetails = this.openDetails.bind(this);
this.onCollectionUpdate = this.onCollectionUpdate.bind(this);

要检查上述声明的重要性, 尝试

<TouchableOpacity
    onPress={this.props.openDetails} // <-- this line
 > ... </>

警告消息是由于状态更新完成前的导航引起的。