How to use React Router with Laravel?

前端 未结 5 2010
温柔的废话
温柔的废话 2020-12-05 10:01

I needing use React Router with a Laravel project.

But when I create router on the React Router and try access, Laravel<

5条回答
  •  Happy的楠姐
    2020-12-05 10:39

    How about using ?

    E.g.

    import React from 'react';
    import {
        HashRouter,
        Route,
        Link
    }from 'react-router-dom';
    import Profile from './Profile';
    
    export default class App extends React.Component {
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                
                    Profile
                    
                
            );
        }
    }
    

    In Laravel's router...

    Route::get('/', function(){
        return view('index'); //This view is supposed to have the react app above
    });
    

    With HashRouter, your client side routing is done with # (Fragment Identifier), which is not read by Laravel's routing (i.e. server side routing)

    Upon arriving this page, the URL is /.

    Clicking the link will make the URL /#/profile and the component will appear.

    After that, if you refresh the page, you wont' see the Route not exist error. This is because, from Laravel's perspective, the URL is still /. (The component Profile still remains there.)

    https://reacttraining.com/react-router/web/api/HashRouter

    Hope my explanation is clear.

提交回复
热议问题