score:3

Accepted answer

yes sure this is possible.

let me give you some background over this. if you are building spa with multiple pages, you would like to use a client-side routing in otder to display differen page based on the browser url. as i know defacto solution for routing in react is react-router but there are many other to chose from. see this link. unfortunately, i won't be able to completely explain how to use react-router solution, but here is a brief example.

inside your main component (like app.jsx) add routing:

// 1. do proper imports
import { browserrouter } from "react-router-dom";

// 2. wrap your application inside of browserrouter component

class app extends react.component {
// your code

    render() {
        return (
            <browserrouter>
                {/* your app code */}

                {/* add routing */}
                <route path="/" exact component={dashboard} />
                <route path="/questions/id/:id" component={question} />

                {/* your app code */}
            <browserrouter>
        )
    }
}

you should see now that when your browser url matches /questions/id/:id component question should be mounted to the page. inside this component, you can fetch id that was passed in. it will be inside this.props.match.param.id property

class question extends react.component {
// your code

    render() {
        const id = this.props.match.param.id;        

        return (
            <div>
                page with { id } was opened
            <div>
        )
    }
}

sure you would like to familiarise yourself with react-router docs.

score:0

reactjs don't include a router. but the most popular package to do this is react-router. the official documentation references other good packages


Related Query

More Query from same tag