React Router v4 和 MatchWithFade 的问题 [英] Trouble with React Router v4 and MatchWithFade

查看:45
本文介绍了React Router v4 和 MatchWithFade 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与这篇文章有关:

React Router v4 使用 React Motion 匹配过渡

...但我认为它值得自己提出问题.

我正在尝试弄清楚如何使用从这里获取的 示例:

https://react-router.now.sh/animated-transitions

我看到的问题是,如果我有两个标签,并且我想在它们之间淡入淡出,我只会看到两个标签之一的淡化效果,这取决于哪个 <MatchWithFade> 首先出现在我的代码中.

相关代码如下:

const 一 = () =>{返回 (<div style={{position:'absolute', top: 0, left: 0, width: 300, backgroundColor: 'orange'}}>一一一一一一一一一一

)}const 二 = () =>{返回 (<div style={{position:'absolute', top: 0, left: 0, width: 300, backgroundColor: 'yellow'}}>二二二二二二二二二二

)}类 AppBody 扩展组件 {使成为 () {返回 (<div style={{position: 'relative'}}><MatchWithFade pattern='/one' component={One}/><MatchWithFade pattern='/two' component={Two}/>

)}}

在这个例子中,导航到 /one,(使用 React Router 组件)会导致淡入淡出,但是如果我导航到 /two,没有淡入淡出.然后,如果我首先列出 <MatchWithFade pattern='/two' .../>,那么我会看到淡入淡出过渡到 /two,而不是 /一个.

仅使用 工作正常,所以我认为这不是我如何配置 的根本问题.

我希望我只是在做一些愚蠢的事情,但对于我的生活,我无法弄清楚是什么.任何指导表示赞赏.

更新

我无法弄清楚如何使用 React Router 制作 jsbin(无法弄清楚如何引用其上的方法和对象,因为我只通过 import 语句使用过 RR).所以这是下一个最好的事情:这是一个完整的例子来演示这个问题:

import React, { Component } from 'react';从 'react-router/BrowserRouter' 导入 BrowserRouter从反应运动"导入 { TransitionMotion, spring }从反应路由器/匹配"导入匹配从反应路由器/链接"导入链接;const MatchWithFade = ({ component:Component, ...rest }) =>{const willLeave = () =>({不透明度:弹簧(0)})返回 (<Match {...rest} children={({matched, ...props }) =>{返回 (<过渡运动willLeave={willLeave}样式={匹配?[ {关键:props.location.pathname,样式:{不透明度:1},数据:道具} ] : []}>{interpolatedStyles =>{返回 (<div>{interpolatedStyles.map(config => (

))}

)}}</TransitionMotion>)}}/>)}const 一 = () =>{返回 (<div style={{position:'absolute', top: 0, left: 0, width: 300, border: '1px solid black', backgroundColor: 'orange', minHeight: 200}}>一一一一一一一一一一<br/>一一一一一一一一一一<br/>

)}const 二 = () =>{返回 (<div style={{position:'absolute', top: 0, left: 0, width: 300, border: '1px solid black', backgroundColor: 'yellow', minHeight: 200}}>二二二二二二二二二二<br/>二二二二二二二二二二<br/>

)}类 App 扩展组件 {使成为 () {返回 (<浏览器路由器><div style={{padding: 12}}><div style={{marginBottom: 12}}><Link to='/one'>One</Link>||<Link to='/two'>Two</Link>

<div style={{position: 'relative'}}><MatchWithFade pattern='/one' component={One}/><MatchWithFade pattern='/two' component={Two}/>

</BrowserRouter>)}}导出默认应用程序;

这个 MatchWithFade 和取自 React Router 文档的只有很小的区别.最大的不同是我提取了 zIndex 引用,但这并没有影响行为.

如果相关,我使用 create-react-app 开始这个项目,我使用的是 React Router 版本 4.0.0-alpha.6.>

解决方案

这是您在 MatchWithFade 示例中应用(或不应用)样式的问题.将 zIndex: 1 添加回您的 willLeave 函数,因为这可确保传出路线在传入路线之上,以便看到不透明度逐渐消失.

然后将绝对定位添加回您要应用样式的包装器 div (styles.fill in the 网站示例) 以便它们可以相互重叠.

这里是已修复代码的要点.

This question is related to this post:

React Router v4 Match transitions using React Motion

...but I thought it deserved its own question.

I'm trying to figure out how to use the <MatchWithFade> example taken from here:

https://react-router.now.sh/animated-transitions

The problem I'm seeing is that if I have two tabs, and I want to fade between them, I'm only seeing the fade effect on one of the two tabs, and it depends on which <MatchWithFade> appears first in my code.

The relevant code is as follows:

const One = () => {
  return (
    <div style={{position:'absolute', top: 0, left: 0, width: 300, backgroundColor: 'orange'}}>
      One one one one one one one one one one
    </div>
  )
}

const Two = () => {
  return (
    <div style={{position:'absolute', top: 0, left: 0, width: 300, backgroundColor: 'yellow'}}>
      Two two two two two two two two two two
    </div>
  )
}

class AppBody extends Component {

  render () {

    return (
      <div style={{position: 'relative'}}>
        <MatchWithFade pattern='/one' component={One} />
        <MatchWithFade pattern='/two' component={Two} />
      </div>
    )
  }
}

In this example, navigating to /one, (using the React Router <Link> component) will cause the fade to happen, but if I navigate to /two, there is no fade. Then, if I list <MatchWithFade pattern='/two' ... /> first, then I see the fade transition to /two, but not /one.

Just using <Match> works fine, so I don't think it's a fundamental issue with how I have <BrowserRouter> configured.

I'm hoping that I'm just doing something silly, but for the life of me, I can't figure out what. Any guidance is appreciated.

UPDATE

I couldn't figure out how to made a jsbin using React Router (couldn't figure out how to reference the methods and objects on it, since I've only ever used RR via import statements). So here's the next best thing: this is a complete example that demonstrates this issue:

import React, { Component } from 'react';
import BrowserRouter from 'react-router/BrowserRouter'

import { TransitionMotion, spring } from 'react-motion'
import Match from 'react-router/Match'

import Link from 'react-router/Link';

const MatchWithFade = ({ component:Component, ...rest }) => {
  const willLeave = () => ({ opacity: spring(0) })

  return (
    <Match {...rest} children={({ matched, ...props }) => {
      return (
        <TransitionMotion
          willLeave={willLeave}
          styles={matched ? [ {
            key: props.location.pathname,
            style: { opacity: 1 },
            data: props
          } ] : []}
        >
          {interpolatedStyles => {
            return (
              <div>
                {interpolatedStyles.map(config => (
                  <div
                    key={config.key}
                    style={{...config.style }}
                  >
                    <Component {...config.data}/>
                  </div>
                ))}
              </div>
            )
          }}
        </TransitionMotion>
      )
    }}/>
  )
}

const One = () => {
  return (
    <div style={{position:'absolute', top: 0, left: 0, width: 300, border: '1px solid black', backgroundColor: 'orange', minHeight: 200}}>
      One one one one one one one one one one<br />
      One one one one one one one one one one<br />
    </div>
  )
}

const Two = () => {
  return (
    <div style={{position:'absolute', top: 0, left: 0, width: 300, border: '1px solid black', backgroundColor: 'yellow', minHeight: 200}}>
      Two two two two two two two two two two<br />
      Two two two two two two two two two two<br />
    </div>
  )
}


class App extends Component {

  render () {
    return (
        <BrowserRouter>
          <div style={{padding: 12}}>
            <div style={{marginBottom: 12}}>
              <Link to='/one'>One</Link> || <Link to='/two'>Two</Link>
            </div>
            <div style={{position: 'relative'}}>
              <MatchWithFade pattern='/one' component={One} />
              <MatchWithFade pattern='/two' component={Two} />
            </div>
          </div>
        </BrowserRouter>
    )
  }
}

export default App;

There are only very minor differences between this MatchWithFade, and the one taken from the React Router docs. The biggest difference is that I pulled out the zIndex reference, but that did not affect the behavior.

If it's relevant, I started this project using create-react-app, and I'm using React Router version 4.0.0-alpha.6.

解决方案

This is an issue with the style you're applying (or not) from the MatchWithFade example. Add zIndex: 1 back to your willLeave function, as this ensures the outgoing route is over top of the incoming in order to see the opacity fade.

Then add the absolute positioning back to the wrapper div you're applying the style to (styles.fill in the website example) so that they can overlap each other.

Here is a gist with your code fixed up.

这篇关于React Router v4 和 MatchWithFade 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆