简体   繁体   English

redux 中的自定义反应钩子返回帮助函数

[英]Custom react hook returning helper functions in redux

Imagine the situation you have prepared this custom hook for handling multistep form.想象一下您已经准备好这个自定义钩子来处理多步表单的情况。 The purpose of creating it was to have an easily reusable way to handle common logic for such use-case.创建它的目的是提供一种易于重用的方法来处理此类用例的通用逻辑。

const [steps, currentStep, {totalSteps, isInvalid, isCompleted, isLastStep, isAllStepsCompleted, setActiveStep, handleStepValidation}] = useSteps(['Set preview information', 'Add some content', 'Adjust general settings'])

following are just some helper functions:以下只是一些辅助函数:

  • totalSteps总步数
  • isInvalid是无效的
  • isCompleted完成了
  • isLastStep是最后一步
  • isAllStepsCompleted isAllStepsCompleted
  • setActiveStep设置活动步骤
  • handleStepValidation处理步骤验证

Now, this hook can be perfectly used standalone without the need for any redux logic straight away in the root component/container component.现在,这个钩子可以完美地独立使用,而无需直接在根组件/容器组件中使用任何 redux 逻辑。 Redux comes in for managing the data (what user filled in the form, etc...). Redux 用于管理数据(用户填写的表格等...)。 For that, I have just a simple reducer.为此,我只有一个简单的减速器。

export default function reducer(state = blogInitialState, action) {
    switch (action.type) {
        case blogActionTypes.BLOG_STORE_TITLE:
            return {...state, data: { ...state.data, title: action.payload}}
        case blogActionTypes.BLOG_STORE_IMAGE:
            return {...state, data: { ...state.data, previewImage: action.payload.previewUrl, previewImageFile: action.payload.file}}
        case blogActionTypes.BLOG_STORE_CONTENT:
            return {...state, }
        case blogActionTypes.BLOG_DELETE_IMAGE_IN_CONTENT:
            return {...state, }
        case blogActionTypes.BLOG_STORE_PUBLICITY_STATUS:
            return {...state, }
        case blogActionTypes.BLOG_STORE_SLUG:
            return {...state, }
        case blogActionTypes.BLOG_SAVE_POST:
            return {...state, }
        default:
            return state
    }
}

Since I want to handle validation of the whole step based on the input data, it feels right to handle the validation inside the reducer, which would mean moving the hook inside the reducer.由于我想根据输入数据处理整个步骤的验证,所以在 reducer 内部处理验证感觉是正确的,这意味着在 reducer 内移动钩子。

So, we are getting into this situation:因此,我们正在进入这种情况:

export default function reducer(state = blogInitialState, action) {
    const [steps, active, {totalSteps, isInvalid, isCompleted, isLastStep, isAllStepsCompleted, setActiveStep, handleStepValidation}] = useSteps(['Set preview information', 'Add some content', 'Adjust general settings'])

    switch (action.type) {
        case blogActionTypes.BLOG_STORE_TITLE:
            return {...state, data: { ...state.data, title: action.payload}}
        case blogActionTypes.BLOG_STORE_IMAGE:
            return {...state, data: { ...state.data, previewImage: action.payload.previewUrl, previewImageFile: action.payload.file}}
        case blogActionTypes.BLOG_STORE_CONTENT:
            return {...state, }
        case blogActionTypes.BLOG_DELETE_IMAGE_IN_CONTENT:
            return {...state, }
        case blogActionTypes.BLOG_STORE_PUBLICITY_STATUS:
            return {...state, }
        case blogActionTypes.BLOG_STORE_SLUG:
            return {...state, }
        case blogActionTypes.BLOG_SAVE_POST:
            return {...state, }
        default:
            return state
    }
}

The main problem now is that the component loses access to the helper functions: isInvalid , isCompleted , ...现在的主要问题是组件无法访问辅助函数: isInvalidisCompleted ,...

Passing the functions via the state doesn't feel right.通过 state 传递功能感觉不对。

Would you have any suggestions on how to resolve this "stalemate"?你对如何解决这个“僵局”有什么建议吗? :) :)

Ok, I think I came out with a solution.好的,我想我想出了一个解决方案。

I shall create a new "common" reducer for handling step logic and combine it with whatever is needed, ie: basicMultistepReducer and blogPostReducer .我将创建一个新的“通用”reducer 来处理步骤逻辑,并将它与任何需要的东西结合起来,即: basicMultistepReducerblogPostReducer

This shall provide some "raw" multistep state into my component, where I load the "raw" state into the hook, which will then provide the helper functions that I can call as usual.这将在我的组件中提供一些“原始”多步 state,我将“原始”state 加载到钩子中,然后它将提供我可以照常调用的辅助函数。

I hope this helps anybody.我希望这对任何人都有帮助。 If I am somewhat unclear or anybody has a better solution, please feel free to write down a comment.如果我有些不清楚或有人有更好的解决方案,请随时写下评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM