我正在使用react-quill
,它的要求是modules
道具必须被缓存并且不应该改变。我正在使用useMemo
钩子来记忆它。对象是:
const modules = useMemo(() => ({
key: {
value: {
handler: handlerFunc
}
}), [])
它的用法如下:
<Quill
modules={modules}
value={value}
onChange={onChange}
// value and onChange are props
/>
handleFunc
在modules
对象中,只是控制台日志value
道具。问题是这value
不是最新的,它是第一个值。我在类组件中尝试了同样的事情,我能够获得价值并且它工作得非常好,但不幸的是它不适用于useMemo
. 请注意,我不能只输入as不应该更改[value]
的第二个参数。useMemo
modules
类组件实现:
class MyComponent extends React.Component {
constructor() {
super()
this.handlerFunc = this.handlerFunc.bind(this)
this.modules = this.initModules(this.handlerFunc)
}
initModules(handlerFunc) {
return {
key: {
value: {
handler: handlerFunc,
},
},
}
}
handlerFunc() {
console.log(this.props.value)
}
}
但我不能使用类组件,因为它是只使用功能组件的要求。有什么办法,我可以有最新value
的useMemo
,或者任何其他的解决方案?