Javascript 将功能部件传递给弹簧&x27;用户转换
我正在尝试编写一个React组件,其中包含一个按钮面板,每个按钮都需要能够根据应用程序的状态独立地进行切换。为此,我创建了一个Javascript 将功能部件传递给弹簧&x27;用户转换,javascript,reactjs,react-functional-component,react-spring,Javascript,Reactjs,React Functional Component,React Spring,我正在尝试编写一个React组件,其中包含一个按钮面板,每个按钮都需要能够根据应用程序的状态独立地进行切换。为此,我创建了一个usetransformation,它包装在一个自定义挂钩中,当它接收到一个新的SVG功能组件时,应该触发转换 我遇到的问题是,虽然按钮当前正在正确转换,但我似乎无法让useTransition在接收到新组件时将其替换掉。当组件被替换为空数组时,它将删除一个,当传入新组件时,钩子内的useffect将触发,但由于某种原因,usetransfit未拾取更改 我猜这与组件之间
usetransformation
,它包装在一个自定义挂钩中,当它接收到一个新的SVG功能组件时,应该触发转换
我遇到的问题是,虽然按钮当前正在正确转换,但我似乎无法让useTransition
在接收到新组件时将其替换掉。当组件被替换为空数组时,它将删除一个,当传入新组件时,钩子内的useffect
将触发,但由于某种原因,usetransfit
未拾取更改
我猜这与组件之间的对象相等有关,但我不知道如何在不使用window.requestAnimationFrame
强制更改两次状态的情况下解决这个问题。这里有一个简单的演示
父组件:
import React, { useState } from "react";
import { a } from "react-spring";
import "./styles.css";
import PlaySVG from "./svgs/PlaySVG";
import SaveSVG from "./svgs/SaveSVG";
import SearchSVG from "./svgs/SearchSVG";
import TrashSVG from "./svgs/TrashSVG";
import { useVertTransition } from "./Hooks";
export default function ActionButtons() {
const svgs = {
play: <PlaySVG style={{ width: "100%", height: "auto" }} id="play" />,
search: <SearchSVG style={{ width: "80%", height: "auto" }} id="search" />,
save: <SaveSVG style={{ width: "80%", height: "auto" }} id="save" />,
trash: <TrashSVG style={{ width: "80%", height: "auto" }} id="trash" />
};
const [actions, setActions] = useState({
one: svgs.play,
two: svgs.search,
three: svgs.trash
});
const [slotOne] = useVertTransition(actions.one);
const [slotTwo] = useVertTransition(actions.two);
const [slotThree] = useVertTransition(actions.three);
function buttonHandler() {
setActions({
...actions,
one: [],
two: svgs.save
});
}
return (
<div className="container">
<div className="panel">
<div className="button-container">
{slotOne.map(({ item, props, key }) => (
<a.button key={key} style={props} onClick={buttonHandler}>
{item}
</a.button>
))}
</div>
<div className="button-container">
{slotTwo.map(({ item, props, key }) => (
<a.button key={key} style={props} onClick={buttonHandler}>
{item}
</a.button>
))}
</div>
<div className="button-container">
{slotThree.map(({ item, props, key }) => (
<a.button key={key} style={props} onClick={buttonHandler}>
{item}
</a.button>
))}
</div>
</div>
</div>
);
}
SVG组件示例:
import React from "react";
function PlaySVG({ style }) {
return (
<svg xmlns="http://www.w3.org/2000/svg" style={style} viewBox="0 0 24 24">
<path d="M7 6L7 18 17 12z" />
</svg>
);
}
export default PlaySVG;
从“React”导入React;
函数PlaySVG({style}){
返回(
);
}
导出默认播放SVG;
您是对的,react spring无法检测按钮之间的差异。但它不检查项目,只检查键。因此,您必须为唯一键添加一些内容。因此,您可能希望将输入从svg更改为svg旁边包含名称的对象
const getSvg = name => ({ name, svg: svgs[name] });
const [actions, setActions] = useState({
one: getSvg("play"),
two: getSvg("search"),
three: getSvg("trash")
});
现在,您可以添加按键功能:
const vertTransition = useTransition(item, svg => svg.name, {
您应该更改渲染:
{slotOne.map(({ item, props, key }) => (
<a.button key={key} style={props} onClick={buttonHandler}>
{item.svg}
</a.button>
))}
{slotOne.map({item,props,key})=>(
我还学到了一个新技巧,我从未使用过order属性。没错,react-spring无法检测按钮之间的差异。但它不检查项目,只检查键。因此,您必须为唯一键添加一些内容。因此,您可能希望将输入从svg更改为包含名称besi的对象对svg进行反编译
const getSvg = name => ({ name, svg: svgs[name] });
const [actions, setActions] = useState({
one: getSvg("play"),
two: getSvg("search"),
three: getSvg("trash")
});
现在,您可以添加按键功能:
const vertTransition = useTransition(item, svg => svg.name, {
您应该更改渲染:
{slotOne.map(({ item, props, key }) => (
<a.button key={key} style={props} onClick={buttonHandler}>
{item.svg}
</a.button>
))}
{slotOne.map({item,props,key})=>(
我还学到了一个新技巧,我从来没有使用过order属性。太棒了,我想我终于开始了解关键系统如何与r-spring配合使用了-非常感谢!太棒了,我想我终于开始了解关键系统如何与r-spring配合使用了-非常感谢!