javascript - 如何从自定义 React 钩子返回变异值?
问题描述
如果我的状态与初始状态相比发生变化,我想将我的组件状态传递给自定义钩子并返回 true。
import { useRef, useEffect } from 'react'
export const useDirtyState = (props:any) => {
//Possibly use local state and set it to true and return that
const isFirstRender = useRef<boolean>(true)
const isDirty = useRef<boolean>(false)
useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false
return
}
isDirty.current = true
console.log(isDirty.current) // is true
}, [props])
console.log(isDirty.current) // I return this and it is false :(
}
//In some other component
const isDirty = useDirtyStaate(state)//Expect this to be true when state is changed
问题是即使道具发生变化,外部console.log也会显示错误,因为我的效果在那之后运行(我猜?)。如何从此钩子返回正确的值?
编辑:我尝试将本地状态添加到钩子并将其设置为 true 并返回它。虽然这种方法有效,但我想知道是否有更清洁的方法,因为它似乎会导致 1 个额外的渲染。
解决方案
只需将原始值存储在 ref 中,并将其与渲染期间提供的值进行比较:
const {useRef, useState} = React;
// You can implement the value comparison using your preferred method
function areValuesEqual (value1, value2) {
return Object.is(value1, value2);
}
/**
* TS:
* function useValueChanged <T>(value: T): boolean
*/
function useValueChanged (value) {
const originalRef = useRef(value);
return !areValuesEqual(originalRef.current, value);
}
function Example () {
const renderCountRef = useRef(0);
renderCountRef.current += 1;
const [count, setCount] = useState(0);
const increment = () => setCount(n => n + 1);
const didChange = useValueChanged(count);
return (
<div>
<div>Render count: {renderCountRef.current}</div>
<div>Changed: {didChange ? 'Yes' : 'No'}</div>
<button onClick={increment}>Clicks: {count}</button>
</div>
);
}
ReactDOM.render(<Example />, document.getElementById('root'));
<script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script>
<div id="root"></div>
推荐阅读
- nltk - 您可以通过 Poetry 下载 NLTK 数据吗?
- python - 同时为多个按钮设置样式
- android - Dagger2 为 Android 中的 ViewModel 提供 ApplicationContext
- r - 在 Linux Mint LMDE4 上安装 R.4.0
- c - 是否有不应用作命令行参数的标志?
- javascript - 如何让文本突出显示跨多个元素工作?
- python-3.x - 在嵌套字典中添加新的(键,值)对 - python3
- javascript - ASP.NET Core - AJAX 数组看起来没有填充并传递给控制器
- google-sheets - Google表格中的堆积柱形图从多列中获取数据
- javascript - 当元素高度取决于该元素内的文本时为元素高度设置动画