代码之家  ›  专栏  ›  技术社区  ›  Emad Dehnavi

React Recompose:无法访问在WithStateProps中创建的方法

  •  0
  • Emad Dehnavi  · 技术社区  · 6 年前

    我使用重新组合来定义以下方法:

    export interface WithStateProps {
      isDisabled: boolean;
      isReady: boolean;
      setDisabled(value: boolean): void;
      setReady(value: boolean): void;
    }
    
    
    export const withStateHoc = withState('isDisabled', 'setDisabled', false);
    export const withIsEligibleStateHoc = withState(
      'isReady',
      'setReady',
      true
    );
    
    export const isReady = (value : string) => {
      return value ? true : false
    };
    
    export type WrappedProps = StepContentProps &
      FormikProps<MyAddress> &
      InjectedIntlProps & 
      AddressFormHandlers & WithStateProps;
    

    当我想使用 setReady 方法我收到此消息: props.setReady is not a function 这是我的代码:

    export const withFormikHoc = withFormik<
      WrappedProps & RouteComponentProps<{}> & InjectedIntlProps & WithStateProps,
      MyAddress
    >({
     handleSubmit: async (values, { props, setSubmitting }) => {
         const addressAlreadyVerified = isReady(values.country);
         if(addressAlreadyVerified) {
            props.setReady(true)
         }
       }
    })
    

    当我盘旋 props.setReady(true) 在vcode中,我可以看到: (method) WithStateProps.setReady(value: boolean): void

    但我知道 props.setReady 不是函数!

    有人知道我在这里遗漏了什么吗?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Sergio Moura    6 年前

    handleSubmit: async (values, { setSubmitting, ...props }) => {
    

    setSubmitting props

    handleSubmit: async (values, { setReady, setSubmitting }) => {
      const addressAlreadyVerified = isReady(values.country);
      if (addressAlreadyVerified) {
        setReady(true)
      }
    }
    

    handleSubmit: async (values, props) => {
      const addressAlreadyVerified = isReady(values.country);
      if (addressAlreadyVerified) {
        props.setReady(true)
      }
    }
    

    设置提交 完全。如果你愿意的话,你可以把它去掉。

        2
  •  0
  •   Emad Dehnavi    6 年前

    嗯,我发现问题,这是我的错误,在我的 compose 我补充说 withFormikHoc 之前 withStateHoc 和; withIsEligibleStateHoc 这就是错误的原因。把他们带来后,第一个问题解决了。