我决定在我的 React js 应用程序中引入 TypeScript。
使用 react-spring 进行坐标插值的组件有问题。
在 TypeScript 之前,我的组件是这样的:
function Card() {
const [props, set] = useSpring(() => (
{
xy: [0, 0],
config: {mass: 10, tension: 550, friction: 140}
}));
const calc = (x, y) => [x - window.innerWidth / 2, y - window.innerHeight / 2]
return (
<div onMouseMove={({clientX: x, clientY: y}) => set({xy: calc(x, y)})}>
<animated.div className="card1" style={{transform: props.xy.interpolate((x,y) => `translate3d(${x / 10}px,${y / 10}px,0)`)}}/>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
一切正常。
打字稿后:
function Card() {
const [props, …Run Code Online (Sandbox Code Playgroud) 在过去的几天里,我一直在尝试不同的 React 动画库,试图找到一种在视图(而不是路由)之间转换的解决方案,其中视图将是包装其他组件的组件等等。
到目前为止我尝试过:
react-transtition-group
react-animations
react-spring
仍然需要尝试 的react-motionTransition...
...还有更多的东西,但是所有这些,都做我需要的,除了一件事...当应用过渡/样式的每个状态时,子组件总是更新,触发重新渲染(顺便说一句,这是有道理的),除非子组件shouldComponentUpdate()返回 false,或者被包装在 PureComponent 内,这两者都不是解决方案,因为您可能(肯定)希望在过渡结束。
所有的示例都达到了它们的目的,但它们都使用功能组件或简单的字符串来进行演示,其中任何一个都不应该关心是否重新渲染,而是一个简单的修改,每次都会记录组件被渲染后,将显示它们在过渡期间渲染多次。
奇怪的是,似乎没有人关心或不知道。我发现关于此事的疑问或问题很少,尽管它非常真实,并且库对此的记录非常少。
请分享您的解决方案以避免此问题。
transition reactjs react-transition-group react-animations react-spring
我是用 Gatsby 和 React JS 构建网站的新手。
我想transform使用.css 基于 css属性制作一个简单的动画react-spring。当我使用styled-components(使用theme.js文件)时,我希望动画根据断点做出响应。
问题是它react-spring不接受数组,而只接受字符串,例如“10px”。
? 响应式样式react-components
假设我有以下定义
const MyDiv = styled(animated.div)`
height: 100px;
${width}
`;
Run Code Online (Sandbox Code Playgroud)
并简单地调用元素
<MyDiv width={[50, 70]} />
Run Code Online (Sandbox Code Playgroud)
因为我已经在我的文件中实现了断点theme.js,所以这很完美(使用来自styled-components 的ThemeProvider)。
? 按钮上的动画
让我们定义弹簧动画如下:
const [showButton, setShowButton] = useState(false);
const clickState = useSpring({
oi: showButton ? 0 : 1,
});
const divAnimation = {
transform: clickState.oi.interpolate(oi => …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 实现一些视差动画react-spring,但我无法找到一种以响应/统一方式分隔容器的方法。
在这里,我制作了一个片段,其中包含在滚动时为一些 div 设置动画的基本视差效果:
<Parallax pages={5}>
<ParallaxLayer offset={0} speed={0.4} style={{ height: "auto" }}>
<div className="box" />
</ParallaxLayer>
<ParallaxLayer offset={0.9} speed={0.6} style={{ height: "auto" }}>
<div className="box" />
</ParallaxLayer>
<ParallaxLayer offset={1.1} speed={0.8} style={{ height: "auto" }}>
<div className="box" />
</ParallaxLayer>
<ParallaxLayer offset={1.9} speed={1} style={{ height: "auto" }}>
<div className="box" />
</ParallaxLayer>
<ParallaxLayer offset={2} speed={1.2} style={{ height: "auto" }}>
<div className="box" />
</ParallaxLayer>
</Parallax>
Run Code Online (Sandbox Code Playgroud)
具有基于其内容的高度的图层会使它们没有响应,因为每个图层都是从指定offset位置开始的。
如果图层设置为指定的视口高度,我猜这会起作用,但这真的是唯一的方法吗?或者我可能误解了这个组件的用法?
我想将这个库用于本地反应,但无法弄清楚如何.react-native的文档链接已损坏.我可以使用该库进行本地反应吗?
我正在尝试使用 React-spring 安装/卸载组件时创建动画:
import { Transition, animated } from 'react-spring'
...
export class CompName extends PureComponent<CompNamePropsType> {
static defaultProps = {
isExpanded: false,
}
render() {
const {
isExpanded,
...props
} = this.props
return (
<section className={styles.block} {...props}>
<Transition
from={{ opacity: 0 }}
enter={{ opacity: 1 }}
leave={{ opacity: 0 }}>
{isExpanded && (style => (
<animated.div style={{ ...style, background: "orange" }}>
<AnotherComp />
</animated.div>
))}
</Transition>
</section>
)
}
}
Run Code Online (Sandbox Code Playgroud)
但这是行不通的,我收到了一个错误children is not a function。我做错了什么,如何使用react-spring包装器在组件安装/卸载上创建动画?
我有一个相当简单的淡入场景,我想控制动画的持续时间。但无法围绕如何实现这一点。
代码摘录:
function BoxA() {
return (
<Spring from={{ opacity: 0.2 }} to={{ opacity: 1 }}>
{props => (
<div
style={{
height: 100,
width: 100,
backgroundColor: "pink",
color: "#fff",
...props
}}
>
1
</div>
)}
</Spring>
);
}
Run Code Online (Sandbox Code Playgroud)
完整代码示例:https : //codesandbox.io/s/n7pw660264
以下来自官方示例的简单组件:
import {useSpring, animated} from 'react-spring'
function App() {
const props = useSpring({opacity: 1, from: {opacity: 0}})
return <animated.div style={props}>I will fade in</animated.div>
}
Run Code Online (Sandbox Code Playgroud)
问题
如何fadeIn再次为效果(或任何其他动画)设置动画,例如当我单击按钮或解决承诺时?
我试图在使用v9.x中的新useTransition 钩子更改react-spring进行过滤时对列表的过渡进行动画处理,以便在过滤列表项时,剩余的项目会移动到新位置。
到目前为止,我已经设法让列表中的组件淡入和淡出,但一旦淡出动画完成,其余组件就会立即跳到新位置。我无法改变这一点。
如何制作剩余组件的动画以顺利移动到新位置?
这是当前代码的代码沙箱链接。
Plum如果您在搜索栏中输入“p”,并观察名称为该名称的组件在短暂延迟后跳跃,则可以最清晰地看到跳跃效果。
App.js
import { useState } from "react";
import { useSpring, useTransition, animated } from "react-spring";
export default function App() {
const [items, setItems] = useState([
{ name: "Apple", key: 1 },
{ name: "Banana", key: 2 },
{ name: "Orange", key: 3 },
{ name: "Kiwifruit", key: 4 },
{ name: "Plum", key: 5 }
]);
const [searchText, setSearchText] = useState("");
const filteredItems = items.filter((item) =>
item.name.toLowerCase().includes(searchText.toLowerCase())
);
const …Run Code Online (Sandbox Code Playgroud) 嗨,我有这个问题,如果我想将 react-spring 安装到我的 React 项目中,它只会弹出一堆依赖项错误和警告,我不知道该怎么办。我试图检查 react-spring 的文档,但找不到任何东西。我在stackoverflow上检查了几页,但仍然没有找到任何有用的东西。有人可以帮忙吗?
npm WARN ERESOLVE overriding peer dependency
npm WARN Found: react@17.0.2
npm WARN node_modules/react-spring/node_modules/react
npm WARN peer react@">=16.8" from @react-spring/konva@9.0.0
npm WARN node_modules/react-spring/node_modules/@react-spring/konva
npm WARN @react-spring/konva@"^9.0.0" from react-spring@9.0.0
npm WARN node_modules/react-spring
npm WARN
npm WARN Could not resolve dependency:
npm WARN peer react@"16.8.x" from react-konva@16.8.6
npm WARN node_modules/react-spring/node_modules/react-konva
npm WARN peer react-konva@">=16.8" from @react-spring/konva@9.0.0
npm WARN node_modules/react-spring/node_modules/@react-spring/konva
npm WARN ERESOLVE overriding peer dependency
npm WARN Found: react@17.0.2
npm WARN node_modules/react-spring/node_modules/react
npm WARN peer react@">=16.8" …Run Code Online (Sandbox Code Playgroud) react-spring ×10
reactjs ×9
animation ×2
javascript ×2
css ×1
gatsby ×1
jsx ×1
npm ×1
npm-install ×1
parallax ×1
react-hooks ×1
react-native ×1
transition ×1
typescript ×1