在我的工作中,我们使用重构,我们使用这种curry函数,这对我来说似乎过于复杂,让我的大脑受到伤害。
谁能用英语解释这是如何工作的?
谢谢
withStateHandlers(({ readAt, isSender }) => ({ trackVisibility: !isSender && !readAt }), {
updateTrackVisibility: () => () => ({
trackVisibility: false,
}),
}),
我知道readAt和isSender来自上面声明的一个片段。最令人困惑的部分是在updateTrackVisibility
之后返回另一个函数的函数???
withStateHandlers(
initialState: Object | (props: Object) => any,
stateUpdaters: {
[key: string]: (state:Object, props:Object) => (...payload: any[]) => Object
}
)
props
是从父母(或上面的HOC)传递的道具
state
是从initialState中创建的对象传递的
payload
params从我们触发函数的地方传递(处理程序)
第一个参数 - ({ readAt, isSender }) => ({ trackVisibility: !isSender && !readAt })
- 创建一个名为trackVisibility的prop(带编码值)
第二个参数 - 添加一个触发时的函数,使trackVisibility
为false
const enhancer = withStateHandlers(({ title, text, maxChars }) => ({ isLongTitle: title.length > maxChars, text }), {
addText: (state, props) => (text) => ({
text: state.text + text,
isLongTitle: state.text.length + text.length > props.maxChars
}),
})
const MyComponent = enhancer(props => {
return (
<input onChange={event => props.addText(event.target.value)} />
)
}