反应导航:动作调度但不导航

时间:2020-03-06 12:14:04

标签: reactjs react-native redux react-navigation

我最近将组件从ES6类更改为函数。我也在使用Redux钩子,但是我的动作出现在Redux记录器中,但是从组件的导航已停止。

有什么主意我该如何解决这个问题?

新组件:

import React, {Component} from 'react';
import {
  .....
} from 'react-native';
import {useDispatch} from 'react-redux';
import {StackActions, NavigationActions} from 'react-navigation';
import CustomImage from './CustomImage';

const VideoEpisode = props => {
  const {id, title, description, video, preview, push, testLink} = props;

  const dispatch = useDispatch();
  return (
    <TouchableHighlight
      ....
      onPress={() => {
        const resetAction = StackActions.reset({
          index: 0,
          actions: [
            NavigationActions.navigate({
              routeName: testLink ? 'TestYourself' : 'VideoPlayer',
              params: {id},
            }),
          ],
        });
        dispatch(resetAction);
      }}>
      ...
    </TouchableHighlight>
  );
};

export default VideoEpisode;

旧组件,使用道具中的dispatch

export default class VideoEpisode extends React.Component {
  constructor(props) {
    super(props);
    this.state = { height: 215 };
  }

  render() {
    const {
      id,
      title,
      description,
      video,
      preview,
      push,
      dispatch,
      testLink
    } = this.props;

    return (
      <TouchableHighlight
        style={styles.episode}
        activeOpacity={1}
        underlayColor="#808080"
        onPress={() => {
          const resetAction = StackActions.reset({
            index: 0,
            actions: [
              NavigationActions.navigate({
                routeName: testLink ? "TestYourself" : "VideoPlayer",
                params: { id }
              })
            ]
          });

          const navigateAction = NavigationActions.navigate({
            routeName: "About"
          });
          dispatch(resetAction);
        }}
      >
        <View style={styles.title}>
          <Text style={styles.text}>{title}</Text>
        </View>
      </TouchableHighlight>
    );
  }
}

0 个答案:

没有答案