简体   繁体   English

React Native-导航器更改路线

[英]React Native - Navigator change routes

I'm using a Navigator object which renders news posts. 我正在使用呈现新闻文章的Navigator对象。

The idea is that you're viewing a single news post, and you can swipe left and right for the next and previous post. 这样的想法是,您正在查看单个新闻帖子,并且可以左右滑动以查看下一个和上一个帖子。 For example, when you swipe right it swipes to the next post. 例如,当您向右滑动时,它会滑动到下一个帖子。 What I want is when I swipe to the next post, I want to load the next post of that post again, so you can keep swiping. 我想要的是当我滑动到下一个帖子时,我想再次加载该帖子的下一个帖子,以便您可以继续滑动。

I give these props: 我给这些道具:

getInitialState: function() {
  return {
    post: {},
    routes: [this.props.prevPost, this.props.post, this.props.nextPost],
    startRoute: this.props.post,
  };
}

My Navigator function looks like this: 我的导航器功能如下所示:

render: function() {
  var self = this;
  return (
    <Navigator
      debugOverlay={false}
      ref={(navigator) => {
        this._navigator = navigator;
      }}
      onDidFocus={this.itemChangedFocus}
      initialRoute={this.state.startRoute}
      initialRouteStack={this.state.routes}
      routeStack={this.state.routes}
      renderScene={this.renderNewsItem}
      configureScene={() => ({
        ...Navigator.SceneConfigs.HorizontalSwipeJump,
      })} />
  );
}

So my idea was onDidFocus , I look at my current post, I then grab the previous and next post and somehow (here's where I'm stuck) trigger a rerender of the navigator component? 所以我的想法是onDidFocus ,我看一下我的当前文章,然后抓取上一个和下一个文章,并且以某种方式(这里是我被卡住的地方)触发导航器组件的重新渲染?

This is how my itemChangedFocus function looks like: 这是我的itemChangedFocus函数的样子:

// Route gives the current news item which has the focus
itemChangedFocus: function(route) {
  // PostStore returns a posts array
  // posts[0] = previousPost, posts[1] = currentPost, posts[2] = nextPost
  let posts = PostStore.getPrevAndNextPost(route);
  // This is probably wrong, but it did replace the nextPost in the route object
  this.props.navigator.route.nextPost = posts[2];
  // Now I need to trigger a rerender? I tried it with setState but that didn't work
}

So the question is, how do I approach this correctly? 所以问题是,我该如何正确处理呢? What am I doing wrong and how can I make sure I can keep swiping in the list? 我做错了什么,如何确定可以继续刷列表?

How about running "forceUpdate" on navigator after setting all props? 设置所有道具后,如何在导航器上运行“ forceUpdate”? That should do the trick (it should call render method on all the scenes in theory). 那应该可以解决问题(理论上应该在所有场景上调用render方法)。

BTW. 顺便说一句。 I think this is not the best way of using navigator for what you want to do - because it does not use the built-in state of Navigator properly. 我认为这不是将导航器用于所需操作的最佳方法-因为它没有正确使用Navigator的内置状态。

If you always want to keep 3 posts rendered (current, prev, next) I think a better solution could be to use replaceAtIndex(route, index) method or even immediatelyResetRouteStack(routeStack) , it should properly modify state for the Navigator and let it render accordingly. 如果您始终希望保留3个帖子的呈现状态(当前,上一个,下一个),我认为一个更好的解决方案可以使用replaceAtIndex(route, index)方法,甚至immediatelyResetRouteStack(routeStack) ,它应该适当地修改Navigator的状态并让它相应地渲染。 One difficulty might be to avoid animation in this case (I am not sure if immediatelyResetRouteStack will cause the animation). 一种困难可能是在这种情况下避免动画处理(我不确定是否立即ResetRouteStack会引起动画处理)。 Maybe you will also need to jumpTo(route) as well after the reset so that no animation is triggered. 也许在重置后,您还需要jumpTo(route) ,以便不触发动画。

If you have limited maximum number of posts at a time on the other hand and you will be able to keep them all rendered, then you do not need to do any magic, you simply need to do push(route) for any new posts and then jumpBack() immediately - it should probably work. 另一方面,如果一次最多只能发布的数量有限,并且可以保留所有帖子,那么您无需做任何魔术,只需对所有新帖子进行push(route) ,然后立即jumpBack() -应该可以使用。

After the swipe forward completes, the didFocus callback will fire. 向前滑动完成后,将触发didFocus回调。 Then you can use navigator.replaceAtIndex(nextRoute, this.state.routes.length) to inject the route for the next scene. 然后,您可以使用navigator.replaceAtIndex(nextRoute, this.state.routes.length)为下一个场景注入路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM