我正在尝试使用新的Hooks从类组件转变为功能组件。但是,感觉useCallback
会获得不必要的子级渲染,这与类组件中的类函数不同。
下面有两个相对简单的片段。第一个是我的示例编写为类,第二个是我的示例重写为功能组件。目的是使功能组件与类组件具有相同的行为。
类组件测试用例
class Block extends React.PureComponent {
render() {
console.log("Rendering block: ", this.props.color);
return (
<div onClick={this.props.onBlockClick}
style = {
{
width: '200px',
height: '100px',
marginTop: '12px',
backgroundColor: this.props.color,
textAlign: 'center'
}
}>
{this.props.text}
</div>
);
}
};
class Example extends React.Component {
state = {
count: 0
}
onClick = () => {
console.log("I've been clicked when count was: ", this.state.count);
}
updateCount = () => {
this.setState({ count: this.state.count + 1});
};
render() {
console.log("Rendering Example. Count: ", this.state.count);
return (
<div style={{ display: 'flex', 'flexDirection': 'row'}}>
<Block onBlockClick={this.onClick} text={'Click me to log the count!'} color={'orange'}/>
<Block onBlockClick={this.updateCount} text={'Click me to add to the count'} color={'red'}/>
</div>
);
}
};
ReactDOM.render(<Example/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id='root' style='width: 100%; height: 100%'>
</div>
功能组件测试用例
const Block = React.memo((props) => {
console.log("Rendering block: ", props.color);
return (
<div onClick={props.onBlockClick}
style = {
{
width: '200px',
height: '100px',
marginTop: '12px',
backgroundColor: props.color,
textAlign: 'center'
}
}>
{props.text}
</div>
);
});
const Example = () => {
const [ count, setCount ] = React.useState(0);
console.log("Rendering Example. Count: ", count);
const onClickWithout = React.useCallback(() => {
console.log("I've been clicked when count was: ", count);
}, []);
const onClickWith = React.useCallback(() => {
console.log("I've been clicked when count was: ", count);
}, [ count ]);
const updateCount = React.useCallback(() => {
setCount(count + 1);
}, [ count ]);
return (
<div style={{ display: 'flex', 'flexDirection': 'row'}}>
<Block onBlockClick={onClickWithout} text={'Click me to log with empty array as input'} color={'orange'}/>
<Block onBlockClick={onClickWith} text={'Click me to log with count as input'} color={'cyan'}/>
<Block onBlockClick={updateCount} text={'Click me to add to the count'} color={'red'}/>
</div>
);
};
ReactDOM.render(<Example/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id='root' style='width: 100%; height: 100%'>
</div>
在第一个(类组件)中,我可以通过红色块更新计数,而无需重新渲染任何一个块,并且可以通过橙色块自由地控制台记录当前计数。
在第二个(功能组件)中,通过红色块更新计数将触发红色和青色块的重新渲染。这是因为useCallback
将创建其功能的新实例,因为计数已更改,从而导致块获得新的onClick
道具并因此重新渲染。橙色块不会重新呈现,因为用于橙色useCallback
的{{1}}不依赖于计数值。这样做很好,但是当您单击橙色块时,它不会显示计数的实际值。
我认为拥有onClick
的意义在于,使子代不会获得具有相同功能的新实例,并且不必进行不必要的重新渲染,但这似乎在回调函数使用第二秒时仍会发生一个变量,根据我的经验,该变量会经常发生。
那么我将如何在不重新渲染子级的情况下在功能组件中实现此useCallback
函数?有可能吗?
更新(解决方案): 使用下面的Ryan Cogswell的答案,我精心制作了一个自定义钩子,以轻松地创建类函数。
onClick
然后我可以像这样轻松地在函数组件中使用它,只需将onClick传递给孩子。它将不再重新渲染子级,而是仍然使用更新的var。
const useMemoizedCallback = (callback, inputs = []) => {
// Instance var to hold the actual callback.
const callbackRef = React.useRef(callback);
// The memoized callback that won't change and calls the changed callbackRef.
const memoizedCallback = React.useCallback((...args) => {
return callbackRef.current(...args);
}, []);
// The callback that is constantly updated according to the inputs.
const updatedCallback = React.useCallback(callback, inputs);
// The effect updates the callbackRef depending on the inputs.
React.useEffect(() => {
callbackRef.current = updatedCallback;
}, inputs);
// Return the memoized callback.
return memoizedCallback;
};
答案 0 :(得分:1)
useCallback
将避免由于父项中发生的某些更改而不必要的子项重新渲染,而这是回调的依赖项中的不部分。为了避免在涉及回调的依赖项时重新渲染子项,您需要使用ref。引用是等效于实例变量的钩子。
下面,我使用onClickMemoized
指向当前onClickRef
(通过onClick
进行设置)的useEffect
,以便它委派给该函数的一个已知版本状态的当前值。
我还更改了updateCount
以使用功能更新语法,因此它不需要依赖count
。
const Block = React.memo(props => {
console.log("Rendering block: ", props.color);
return (
<div
onClick={props.onBlockClick}
style={{
width: "200px",
height: "100px",
marginTop: "12px",
backgroundColor: props.color,
textAlign: "center"
}}
>
{props.text}
</div>
);
});
const Example = () => {
const [count, setCount] = React.useState(0);
console.log("Rendering Example. Count: ", count);
const onClick = () => {
console.log("I've been clicked when count was: ", count);
};
const onClickRef = React.useRef(onClick);
React.useEffect(
() => {
onClickRef.current = onClick;
},
[count]
);
const onClickMemoized = React.useCallback(() => {
onClickRef.current();
}, []);
const updateCount = React.useCallback(() => {
setCount(prevCount => prevCount + 1);
}, []);
return (
<div style={{ display: "flex", flexDirection: "row" }}>
<Block
onBlockClick={onClickMemoized}
text={"Click me to log with empty array as input"}
color={"orange"}
/>
<Block
onBlockClick={updateCount}
text={"Click me to add to the count"}
color={"red"}
/>
</div>
);
};
ReactDOM.render(<Example />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id='root' style='width: 100%; height: 100%'>
</div>
当然,钩子的优点在于您可以将这种有状态逻辑分解为自定义钩子:
import React from "react";
import ReactDOM from "react-dom";
const Block = React.memo(props => {
console.log("Rendering block: ", props.color);
return (
<div
onClick={props.onBlockClick}
style={{
width: "200px",
height: "100px",
marginTop: "12px",
backgroundColor: props.color,
textAlign: "center"
}}
>
{props.text}
</div>
);
});
const useCount = () => {
const [count, setCount] = React.useState(0);
const logCount = () => {
console.log("I've been clicked when count was: ", count);
};
const logCountRef = React.useRef(logCount);
React.useEffect(
() => {
logCountRef.current = logCount;
},
[count]
);
const logCountMemoized = React.useCallback(() => {
logCountRef.current();
}, []);
const updateCount = React.useCallback(() => {
setCount(prevCount => prevCount + 1);
}, []);
return { count, logCount: logCountMemoized, updateCount };
};
const Example = () => {
const { count, logCount, updateCount } = useCount();
console.log("Rendering Example. Count: ", count);
return (
<div style={{ display: "flex", flexDirection: "row" }}>
<Block
onBlockClick={logCount}
text={"Click me to log with empty array as input"}
color={"orange"}
/>
<Block
onBlockClick={updateCount}
text={"Click me to add to the count"}
color={"red"}
/>
</div>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<Example />, rootElement);