我正在尝试将<canvas>
我在这里找到的这个很酷的动画转换为React可重用组件.看起来这个组件需要一个父组件用于画布,并且需要许多子组件function Ball()
.
出于性能原因,制作Balls
无状态组件可能会更好,因为它们会有很多.我对制作无状态组件并不熟悉,并且想知道我应该在哪里定义this.update()
和定义的this.draw
函数function Ball()
.
无状态组件的功能是在组件内部还是外部?换句话说,以下哪项更好?
1:
const Ball = (props) => {
const update = () => {
...
}
const draw = () => {
...
}
return (
...
);
}
Run Code Online (Sandbox Code Playgroud)
2:
function update() {
...
}
function draw() {
...
}
const Ball = (props) => {
return (
...
);
}
Run Code Online (Sandbox Code Playgroud)
各自的优点/缺点是什么,并且对于像我这样的特定用例更好?
Mar*_*olo 51
首先要注意的是无状态功能组件不能有方法,如果它是无状态功能组件,则不应指望调用update
或draw
渲染Ball
.
在大多数情况下,您应该在组件函数之外声明函数,因此您只需声明它们一次并始终重用相同的引用.在内部声明函数时,每次渲染组件时,都会再次定义函数.
在某些情况下,您需要在组件内部定义一个函数,例如,将其指定为基于组件属性的行为不同的事件处理程序.但是你仍然可以在外部定义函数Ball
并将其与属性绑定,使代码更清晰,并使函数update
或draw
函数可重用.
// You can use update somewhere else
const update (propX, a, b) => { ... };
const Ball = props => (
<Something onClick={update.bind(null, props.x)} />
);
Run Code Online (Sandbox Code Playgroud)
代替:
const Ball = props => {
const onClick = useCallback((a, b) => {
// do something with a, b and props.x
}, [props.x]);
return (
<Something onClick={onClick} />
);
}
Run Code Online (Sandbox Code Playgroud)
如果要在函数中使用 props 或组件状态,则应在组件中使用 useCallback 定义。
function Component(props){
const onClick=useCallback(()=>{
// Do some things with props or state
},[])
return <Something {...{onClick}} />
}
Run Code Online (Sandbox Code Playgroud)
另一方面,如果您不想在函数中使用 props 或 state,请在组件外部定义它。
const computeSomethings=()=>{
// Do some things with params or side effects
}
function Component(props){
return <Something {...{onClick}} />
}
Run Code Online (Sandbox Code Playgroud)
对于 HTML 标签,您不需要 useCallback 因为它将在反应端处理并且不会分配给 HTML
function Component(props){
const onClick=()=>{
// Do some things with props or state
}
return <Something {...{onClick}} />
}
Run Code Online (Sandbox Code Playgroud)
小智 8
我们可以在无状态功能组件中包含函数,下面是示例:
const Action = () => {
function handlePick(){
alert("test");
}
return (
<div>
<input type="button" onClick={handlePick} value="What you want to do ?" />
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
但是,这不是一个好习惯,因为handlePick()
每次调用该组件时都会定义函数。
小智 5
我们可以useCallback
在功能组件中使用如下所示的 React 钩子:
const home = (props) => {
const { small, img } = props
const [currentInd, setCurrentInd] = useState(0);
const imgArrayLength = img.length - 1;
useEffect(() => {
let id = setInterval(() => {
if (currentInd < imgArrayLength) {
setCurrentInd(currentInd => currentInd + 1)
}
else {
setCurrentInd(0)
}
}, 5000);
return () => clearInterval(id);
}, [currentInd]);
const onLeftClickHandler = useCallback(
() => {
if (currentInd === 0) {
}
else {
setCurrentInd(currentInd => currentInd - 1)
}
},
[currentInd],
);
const onRightClickHandler = useCallback(
() => {
if (currentInd < imgArrayLength) {
setCurrentInd(currentInd => currentInd + 1)
}
else {
}
},
[currentInd],
);
return (
<Wrapper img={img[currentInd]}>
<LeftSliderArrow className={currentInd > 0 ? "red" : 'no-red'} onClick={onLeftClickHandler}>
<img src={Icon_dir + "chevron_left_light.png"}></img>
</LeftSliderArrow>
<RightSliderArrow className={currentInd < imgArrayLength ? "red" : 'no-red'} onClick={onRightClickHandler}>
<img src={Icon_dir + "chevron_right_light.png"}></img>
</RightSliderArrow>
</Wrapper>);
}
export default home;
Run Code Online (Sandbox Code Playgroud)
我从它的父级获取 'img',这是一个数组。
归档时间: |
|
查看次数: |
35346 次 |
最近记录: |