1

I am making a website with ReactJS and I am using react-router v4 to navigate through website pages. I am trying to use nested routes to achieve the same result as shown in the diagram I uploaded. To be more clear, I want to have some kind of layout component where the header and footer stay the same, and only the content in the middle is changed with every click to the next or previous button. The 'home_page' and the 'create_record' page have the same layout, and the 3 step pages share another layout from the home_page. It's like the Airbnb style layout and navigation.

Any suggestion on how can I achieve this?

enter image description here

1

1 Answer 1

2

Here is my solution. This might not so effective but this is working on me.

So my strategy is get the window url and check the step.

In here I create 8 files.

  • Header
  • Footer
  • Home.js
  • NewRecord.js
  • Steps.js
  • StepOne.js
  • StepTwo.js
  • StepThree.js

I will describe only the App.js and the Steps.js. Others is up to you.

App.js

import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import Home from './Home'
import NewRecord from './NewRecord'
import Steps from './Steps'

const App = () => {

return (
    <Router>
        <Switch>
            <Route exact path="/" component={Home}/>
            <Route exact path="/new_record" component={NewRecord}/>
            <Route path="/new_record/step_1" component={Steps}/>
            <Route path="/new_record/step_2" component={Steps}/>
            <Route path="/new_record/step_3" component={Steps}/> 
        </Switch>
    </Router>
    )

}

export default App

Steps.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

import Header from './Header';
import Footer from './Footer';

import StepOne from './StepOne';
import StepTwo from './StepTwo';
import StepThree from './StepThree';

export default class Steps extends Component {
    constructor (props) {
        super(props)
        this.state = {
            url : window.location.href, 
            currentStep: 0 
        }
    }

    checkCurrentStep = () => {
        // check the url using javascript search() method
        if (this.state.url.search('step_1') != -1) {
            this.state.currentStep = 1;
        } else if (this.state.url.search('step_2') != -1) {
            this.state.currentStep = 2;
        } else {
            this.state.currentStep = 3;
        }
    }

    componentWillMount () {
        this.checkCurrentStep()
    }

    render() {
        return (
            <div>
                <Header />
                { (this.state.currentStep == 1) ? <StepOne /> : 
                  (this.state.currentStep == 2) ? <StepTwo /> :
                  <StepThree />
                }
                <Footer />
            </div>
        );
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is nice one, i tried and the problem is that the url : window.location.href inside the Steps component, doesn't updated, but the url in browser actually updated, so the componentWillMount() method doesn't invoked at all.
@RoStel when do you need to update the url? But is it work if you try each path /new_record/step_1 etc in browser ?
Yes it is workds if you type each url in the browser, but when url changed the componentWillMount() doesn't invoked so the view cannot changed.
what if you change the componenWillMount to componentWillUpdate or componentWillReceiveProps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.