【问题标题】:Recompose withHandlers callback is not triggersRecompose withHandlers 回调不是触发器
【发布时间】:2017-10-13 15:41:47
【问题描述】:

我正在尝试执行这样的验证:

 const validateWithState = compose(
     withState('current', 'handleChange', {}),
     withState('isValid', 'validate', false),
     withHandlers({
        handleChange: ({current, handleChange, validate}) => () => {
              /* logic here */
              handleChange(current, () => {
                   validate() /* <--- here */
              })
        },
        validate: ({current}) => () => {
             /* this line is never reached */
        }
    })
 )

由于某种原因,validate 处理程序从未执行。

想法?

【问题讨论】:

    标签: reactjs recompose


    【解决方案1】:
     const validateWithState = compose(
         withState('current', 'handleChange', {}),
         withState('isValid', 'validate', false),
         withHandlers({
            validate1: () => () => {
            }
         }),
         withHandlers({
            handleChange: ({current, handleChange, validate1}) => () => {
                  /* logic here */
                  handleChange(current, () => {
                       validate1() /* <--- here */
                  })
            },
        })
     )
    

    可能我们无法访问在同一 withHandlers 中定义的函数。这有点多余,但如果我们有以前的withHandlers,我们可以访问。

    【讨论】:

    • 谢谢你!我已经找到了答案,但这正是我想要的!
    猜你喜欢
    • 2017-08-15
    • 2017-12-24
    • 2018-02-22
    • 2019-03-05
    • 2017-08-31
    • 2023-03-27
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    相关资源
    最近更新 更多