React 实现 Step组件

简介

本文将会实现步骤条组件功能。步骤条在以下几个方面改进。

1、将url与Step组件绑定,做到浏览器刷新,不会重定向到Step 1

2、通过LocalStorage 存储之前的Step,做到不丢失数据。

实现

Step.jsx (组件)

javascript 复制代码
import {useEffect, useState} from "react";

export const Step = ({name, data})=>{
    const submit = (event)=>{
        event.preventDefault();
       const local =  localStorage.getItem(name);
       console.log(JSON.parse(local))
    }

    const [current, setCurrent] = useState(0);
    useEffect(()=>{
        let paths = window.location.pathname.split('/');
        setCurrent(parseInt(paths[paths.length - 1]));
    }, [])

    return (
        <form className={'Step'} onSubmit={submit}>
            <div className={'Step-Header'}>
                <div>
                {
                    data.map((item, idx) =>{
                        return <a key={idx} href= {`/step/${idx}`} style={{paddingRight:30}}>{item.name + ((idx === current) ? '√':'')}</a>;
                    })
                }
                </div>
            </div>
            <div className={'Step-Content'}>
                {data[current].content}
            </div>
            <div className={'Step-Footer'}>
                {current > 0 && <button onClick={()=>setCurrent(current-1)}>pre</button>}
                {current + 1 < data.length && <button onClick={()=> setCurrent(current+1)}>next</button>}
                {current === data.length - 1 && <button type="submit">提交</button>}
            </div>
        </form>

    );
}
  1. Step会获取浏览器url中的步骤数,并设置Step-Content。

2.表单在最后一个步骤会有提交按钮,会从local storage中获取表单参数

3.step header 是导航栏, step content是具体的内容,step footer为步骤条操作按钮。

app.jsx (使用)

javascript 复制代码
unction App() {
   const stepName = 'Demo';
   const Step1 = ()=>{
       const local = localStorage.getItem(stepName);

       const [username, setUsername] = useState(local ? local.username:'');
       const change = (event)=>{
           setUsername(event.target.value);

           localStorage.setItem(stepName, JSON.stringify({
               username: event.target.value
           }));
       }

       return <>
           <label htmlFor='username'>用戶名:</label><input type={'text'}  value={username} onChange={change}/>
       </>;
   }
   const steps = [
       {
           name: "步驟1",
           content: <Step1/>
       },
       {
           name: "步驟2",
           content: (<span>2号</span>)
       }
   ]
    return <Step  data={steps} name={stepName} />
}

export default App;

1.Step1组件需要将表单数据与localStorage绑定

相关推荐
MrSkye14 分钟前
🔥React 新手必看!useRef 竟然不能触发 onChange?原来是这个原因!
前端·react.js·面试
古夕2 小时前
my-first-ai-web_问题记录01:Next.js的App Router架构下的布局(Layout)使用
前端·javascript·react.js
程序员小续3 小时前
再也不写 Class:React Hooks 完整实战与最佳实践
前端·javascript·react.js
CF14年老兵5 小时前
从卡顿到飞驰:我是如何用WebAssembly引爆React性能的
前端·react.js·trae
江城开朗的豌豆5 小时前
拆解Redux:从零手写一个状态管理器,彻底搞懂它的魔法!
前端·javascript·react.js
伍哥的传说11 小时前
Mitt 事件发射器完全指南:200字节的轻量级解决方案
vue.js·react.js·vue3·mitt·组件通信·事件管理·事件发射器
随笔记20 小时前
react-router里的两种路由方式有什么不同
前端·react.js
晴空雨1 天前
React 合成事件原理:从事件委托到 React 17 的重大改进
前端·react.js
@大迁世界1 天前
useCallback 的陷阱:当 React Hooks 反而拖了后腿
前端·javascript·react.js·前端框架·ecmascript
Fantastic_sj1 天前
React 19 核心特性
前端·react.js·前端框架