总的来说,这是我第一次使用 React/进行前端开发,我一直在尝试为我的 Web 应用程序创建一个多步骤表单/向导。我参考了 this react hook form tutorial on making a multi step form with validation,但是,每当我尝试提交表单的每个步骤并推送到下一步时,都会不断抛出类型错误。我一定遗漏了一些非常基本的东西。
我的结构如下。
我在 App.js 组件中创建了我的全局存储,并使用 statemachineprovider 包装了我的 App.js 的内容,如下所示
App.js
function Main() {
return (
<StateMachineProvider>
<DevTool/>
<MemoryRouter>
<MainNavbar></MainNavbar>
<div style={{
height: "100vh",
backgroundSize: 'contain',
backgroundPosition: 'top center',
backgroundRepeat: 'no-repeat',
backgroundImage: `url("https://www.smartnation.gov.sg/images/default-source/module/home-base-item/cb0c06c1-cfc1-48a9-84ae-7909e93cf716.jpg" )`
}}>
<div className="content">
<Route exact path="/" component={Home}/>
<Route path="/stuff" component={Stuff}/>
<Route path="/contact" component={Contact}/>
<Route path="/step1" component={Step1}/>
<Route path="/step2" component={Step2}/>
<Route path="/result" component={Result}/>
</div>
</div>
</MemoryRouter>
</StateMachineProvider>
);
}
我的Contact.js 看起来像这样
import React, {Component} from "react";
import ReactDOM from "react-dom";
import {
MemoryRouter,
Route,
Link,
useLocation
} from "react-router-dom";
import Step1 from "./Step1";
import Step2 from "./Step2";
import Result from "./Result";
const Contact = () => {
const location = useLocation();
return (
<>
<div>
<nav className="container" aria-label="form-navigation">
<ul className="pagination">
<li className={location.pathname === "/contact" ? "page-item disabled" : "page-item"}>
<a className="page-link"> <Link to="/step1">Previous</Link></a>
</li>
<li className={location.pathname === "/step1" ? "page-item active" : "page-item"}>
<a className="page-link"> <Link to="/step1">1</Link></a>
</li>
<li className={location.pathname === "/step2" ? "page-item active" : "page-item"}>
<a className="page-link"> <Link to="/step2">2</Link></a>
</li>
<li className={location.pathname === "/result" ? "page-item active" : "page-item"}>
<a className="page-link"> <Link to="/result">3</Link></a>
</li>
<li className="page-item">
<a className="page-link" >Next</a>
</li>
</ul>
</nav>
</div>
</>
);
}
export default Contact;import React, {Component} from "react";
import ReactDOM from "react-dom";
import {
MemoryRouter,
Route,
Link,
useLocation
} from "react-router-dom";
import Step1 from "./Step1";
import Step2 from "./Step2";
import Result from "./Result";
const Contact = () => {
const location = useLocation();
return (
<>
<div>
<nav className="container" aria-label="form-navigation">
<ul className="pagination">
<li className={location.pathname === "/contact" ? "page-item disabled" : "page-item"}>
<a className="page-link"> <Link to="/step1">Previous</Link></a>
</li>
<li className={location.pathname === "/step1" ? "page-item active" : "page-item"}>
<a className="page-link"> <Link to="/step1">1</Link></a>
</li>
<li className={location.pathname === "/step2" ? "page-item active" : "page-item"}>
<a className="page-link"> <Link to="/step2">2</Link></a>
</li>
<li className={location.pathname === "/result" ? "page-item active" : "page-item"}>
<a className="page-link"> <Link to="/result">3</Link></a>
</li>
<li className="page-item">
<a className="page-link" >Next</a>
</li>
</ul>
</nav>
</div>
</>
);
}
export default Contact;
其余代码(Step1、Step2、updateAction 等)与教程中使用的代码完全相同。它也可以在这里找到in codesandbox。
如果有人能解释我的思维错误,我将不胜感激,因为我对状态管理总体上还是很陌生!
答案 0 :(得分:1)
这是由于 LSM 主要版本更新。 https://github.com/bluebill1049/little-state-machine/releases/tag/v4.0.0-rc.2
您需要更改的只是 useStateMachine
用法。
- const { state, action } = useStateMachine(updateAction);
+ const { state, actions } = useStateMachine({ updateAction });