简体   繁体   English

React stepzilla 中的 React 路由器

[英]React router in react stepzilla

I am using react stepzilla to create a step wizzard for my forms.我正在使用React stepzilla为我的表单创建一个 step wizzard。 Now, I need to show the URL path for each step in the browser.现在,我需要在浏览器中显示每个步骤的 URL 路径。 By default stepzilla does not show the URL paths.默认情况下,stepzilla 不显示 URL 路径。

Is it possible to include React router and include the stepzilla component in the routes?是否可以包含 React 路由器并在路由中包含 stepzilla 组件?

Here is my component that defined the steps and render the react stepzilla component:这是我的组件,它定义了步骤并呈现了反应 stepzilla 组件:

 const StepNavigation = (props) => { const steps = [ { name: '1', component: <Intro {...props} /> }, { name: '2', component: <UserForm {...props} /> }, { name: '3', component: <Finish {...this.props} /> }, ]; return ( <div className="stepNavigation"> <StepZilla steps={steps} preventEnterSubmission /> </div> ); }; export default StepNavigation;

Here's one way to do it:这是一种方法:

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Switch, Route } from "react-router-dom";
import StepZilla from "react-stepzilla";

const Step = ({ name }) => {
  return <h1>Step:{name}</h1>;
};

const steps = [
  { name: "Step1", component: <Step name="One" /> },
  { name: "Step2", component: <Step name="Two" /> }
];

function App({ history }) {
  const handleChange = step => {
    history.push({
      pathname: "/",
      search: `?step=${step}`
    });
  };

  return (
    <div className="App">
      <StepZilla steps={steps} onStepChange={handleChange} />
    </div>
  );
}

const RoutedApp = () => (
  <BrowserRouter>
    <Switch>
      <Route path="/" render={App} />
    </Switch>
  </BrowserRouter>
);

const rootElement = document.getElementById("root");
ReactDOM.render(<RoutedApp />, rootElement);

CodeSandbox example here .此处的 CodeSandbox 示例。

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

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