动画转换示例provided in the v4 docs对我来说似乎有点费解,因为它描绘了淡入淡出相同的组件并调整背景颜色。
我正在尝试将这种技术应用到一个更真实的例子中,将一个组件淡出另一个组件,但是我无法让它正常工作(它似乎只是淡出第一个组件,然后是第二个弹出,并且此转换仅以一种方式工作(后退按钮不会转换)。
这是我的代码,它使用示例中的精简版MatchWithFade
:
import React from 'react';
import { TransitionMotion, spring } from 'react-motion'
import { HashRouter, Match, Miss } from 'react-router';
import Home from './components/Home';
import Player from './components/Player';
import FormConfirmation from './components/FormConfirmation';
const App = () => (
<HashRouter>
<div className="App">
<MatchWithFade exactly pattern="/" component={Home} />
<MatchWithFade pattern="/player/:playerId" component={Player} />
<MatchWithFade pattern="/entered" component={FormConfirmation} />
<Miss render={(props) => (
<Home players={Players} prizes={Prizes} {...props} />
)} />
</div>
</HashRouter>
);
const MatchWithFade = ({ component:Component, ...rest }) => {
const willLeave = () => ({ zIndex: 1, opacity: spring(0) })
return (
<Match {...rest} children={({ matched, ...props }) => (
<TransitionMotion
willLeave={willLeave}
styles={matched ? [ {
key: props.location.pathname,
style: { opacity: spring(1) },
data: props
} ] : []}
>
{interpolatedStyles => (
<div>
{interpolatedStyles.map(config => (
<div
key={config.key}
style={{...config.style}}
>
<Component {...config.data}/>
</div>
))}
</div>
)}
</TransitionMotion>
)}/>
)
}
export default App;
我意识到这个问题几乎与this one重复,但是有一个已经接受的答案实际上没有回答这个问题。
答案 0 :(得分:1)
react-motion
不适用于大型应用程序。请改用react-transition-group
。
因为react-motion
使用javascript
来执行过渡。进行API
调用时,过渡将跨越路径(js
中的同步调用),并在过渡到新页面时使过渡变慢。在react-transition-group
中使用CSS
执行转换。
答案 1 :(得分:0)
v4 docs已移至react-transition-group
,因此您可能会考虑这样做。
关于&#34;淡入淡出相同组件&#34;它与Component的实例不完全相同。两个实例同时存在并且可以提供交叉淡入淡出。 react-motion
docs说&#34; TransitionMotion
已经c
保持在{34}左右。我想react-transition-group
是一样的。
答案 2 :(得分:0)
这是使用来自“react-addons-css-transition-group”的react 16和ReactCSSTransitionGroup的更新解决方案
<强> index.js 强>
import React, { Component } from "react";
import ReactCSSTransitionGroup from "react-addons-css-transition-group";
import { render } from "react-dom";
import "./main.css";
function* continuosArrayIterator(arr) {
let idx = 0;
while (idx < arr.length) {
let ret = arr[idx];
idx++;
if (idx === arr.length) {
idx = 0;
}
yield ret;
}
}
class App extends Component {
constructor() {
super();
this.clickHandler = this.clickHandler.bind(this);
this.items = [
{
id: 1,
text: "item1",
img: "https://mirrors.creativecommons.org/presskit/icons/cc.large.png"
},
{
id: 2,
text: "item2",
img: "https://mirrors.creativecommons.org/presskit/icons/by.large.png"
},
{
id: 3,
text: "item3",
img: "https://mirrors.creativecommons.org/presskit/icons/nc.large.png"
},
{
id: 4,
text: "item4",
img:
"https://mirrors.creativecommons.org/presskit/icons/nc-eu.large.png"
}
];
this.imageIterator = continuosArrayIterator(this.items);
this.state = {
image: this.imageIterator.next().value
};
}
clickHandler(event) {
return this.setState({
image: this.imageIterator.next().value
});
}
render() {
return (
<div>
<button onClick={this.clickHandler}>Next Image</button>
<ReactCSSTransitionGroup
transitionAppear={true}
transitionLeaveTimeout={500}
transitionEnterTimeout={500}
className="container"
transitionName="example"
>
<div
key={this.state.image.id}
style={{
position: "absolute",
backgroundImage: `url(${this.state.image.img}`,
backgroundSize: "auto 100px",
height: "100px",
width: "100px"
}}
/>
</ReactCSSTransitionGroup>
</div>
);
}
}
render(<App />, document.getElementById("root"));
<强>的main.css 强>
.container {
position: absolute;
}
.example-enter {
opacity: 0.01;
}
.example-enter.example-enter-active {
opacity: 1;
transition: opacity 500ms ease-in;
}
.example-leave {
opacity: 1;
}
.example-leave.example-leave-active {
opacity: 0.01;
transition: opacity 500ms ease-in;
}
.example-appear {
opacity: 0.01;
}
.example-appear.example-appear-active {
opacity: 1;
transition: opacity 0.5s ease-in;
}
答案 3 :(得分:0)
我也不喜欢RRv4示例的动画效果。我最终为RRv4 Native编写了自己的“ AnimatedSwitch”。
我想将原始动画换成react-spring并启动并运行不会花费太多的工作。
https://javascriptrambling.blogspot.com/2020/07/react-router-native-animatedswitch.html