【发布时间】:2022-12-22 20:06:21
【问题描述】:
大家好,我一直在使用“WebDev Simplified”中的这个钩子
import { ReactElement, useState } from 'react';
export function useMultistepForm(steps: ReactElement[], initialStep = 0) {
const [currentStepIndex, setCurrentStepIndex] = useState(initialStep);
function next() {
setCurrentStepIndex((i) => {
if (i >= steps.length - 1) return i;
return i + 1;
});
}
function back() {
setCurrentStepIndex((i) => {
if (i <= 0) return i;
return i - 1;
});
}
function goTo(index: number) {
setCurrentStepIndex(index);
}
return {
currentStepIndex,
step: steps[currentStepIndex],
steps,
numberOfSteps: steps.length,
isFirstStep: currentStepIndex === 0,
isLastStep: currentStepIndex === steps.length - 1,
goTo,
next,
back,
};
}
所以我想做的是找出一种方法将 goTo() 函数传递给最后一个元素是 Steps 这就像一个摘要,这样我就可以有一些链接或按钮将用户带到那个特定页面,让他们在那里修改一些东西。
我读过 React.cloneElement 可以使用,但我也在 react 文档中看到“使用 cloneElement 并不常见并且会导致代码脆弱。”所以任何建议都会很好。
【问题讨论】:
标签: reactjs react-hooks