你怎么从重构中读出这个咖喱的功能..我的大脑疼

问题描述 投票:2回答:1

在我的工作中,我们使用重构,我们使用这种curry函数,这对我来说似乎过于复杂,让我的大脑受到伤害。

谁能用英语解释这是如何工作的?

谢谢

  withStateHandlers(({ readAt, isSender }) => ({ trackVisibility: !isSender && !readAt }), {
    updateTrackVisibility: () => () => ({
      trackVisibility: false,
    }),
  }),

我知道readAt和isSender来自上面声明的一个片段。最令人困惑的部分是在updateTrackVisibility之后返回另一个函数的函数???

reactjs functional-programming currying recompose
1个回答
1
投票

来自recompose API

withStateHandlers(
  initialState: Object | (props: Object) => any,
  stateUpdaters: {
    [key: string]: (state:Object, props:Object) => (...payload: any[]) => Object
  }
)

props是从父母(或上面的HOC)传递的道具

state是从initialState中创建的对象传递的

payload params从我们触发函数的地方传递(处理程序)

Which means in your code:

第一个参数 - ({ readAt, isSender }) => ({ trackVisibility: !isSender && !readAt }) - 创建一个名为trackVisibility的prop(带编码值)

第二个参数 - 添加一个触发时的函数,使trackVisibility为false

Another (contrived) example:

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)} />
  )
}
© www.soinside.com 2019 - 2024. All rights reserved.