Reputation: 65
I'm trying to build a Laravel project using ReactJS. I've done show the data on table. However, when I add some links like Create, Edit, Delete... , it occurred some errors:
- Failed context type: The context `router` is marked as required in `Link`, but its value is `undefined`.
- Uncaught Error: You should not use `Link` outside a `Router`
So:
Im new so please help me. Thank you. Here's my code:
Example.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
import {Link} from "react-router-dom";
import CreatePost from './CreatePost';
import RoutePath from '../routes/RoutePath';
...
postRow(p){
return (
<tr key = {p.id}>
<td>{p.title}</td>
<td>{p.description}</td>
<td><Link to='/edit'></Link></td>
<td><Link to='/delete'></Link></td>
</tr>
)
}
render() {
const posts = this.state.posts.map(post => this.postRow(post));
return (
<div>
<table border="1">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{ posts }
</tbody>
</table>
<div>
<Link to='/add-post'>Add</Link>
</div>
</div>
);
}
}
ReactDOM.render(
<Example />
, document.getElementById('homepage')
);
RoutePath.js
import React, {Component} from 'react';
import { Route, BrowserRouter } from "react-router-dom";
import CreatePost from '../components/CreatePost';
import Example from '../components/Example';
export default class RoutePath extends Component{
render(){
return(
<BrowserRouter>
<div>
<Route exact path='/' component={Example}/>
<Route path='/add-post' component={CreatePost}/>
<Route path='/edit' component={Edit}/>
</div>
</BrowserRouter>
)
}
}
Upvotes: 2
Views: 4849
Reputation: 2606
To add on to what @manley13 suggested. You can also do it in one get
call:
Route::get('/{route?}',function(){
return view('home');
});
The ?
makes the route
optional.
Upvotes: 0
Reputation: 1435
This works for me... My single view for my Single-Page-App is home.blade.php
and I am using BrowserRouter
in my App.js
Route::get('/', function () {
return view('home');
});
Route::get('/{route}',function(){
return view('home');
});
This will allow you to go to http://localhost/ and get the homepage of the app or http://localhost/about-us and get the 'about-us' route.
This is a better method than using HashRouter which would look a little uglier: http://localhost/#/about-us
Upvotes: 6
Reputation: 623
RoutePath
, you need to import Switch
from
'react-router-dom'
at first.Example
, otherwise you should render on RoutePath
RoutePath
in your app.js
.require('./homeComponents/RoutePath');
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { Route, BrowserRouter, Switch } from "react-router-dom";
import CreatePost from '../components/CreatePost';
import Example from '../components/Example';
export default class RoutePath extends Component{
render(){
return(
<BrowserRouter>
<Switch>
<Route exact path='/' component={Example}/>
<Route path='/add-post' component={CreatePost}/>
<Route path='/edit' component={Edit}/>
</Switch>
</BrowserRouter>
)
}
}
ReactDOM.render(
<RoutePath />
, document.getElementById('homepage')
);
import React, { Component } from 'react';
import axios from 'axios';
import {Link} from "react-router-dom";
import CreatePost from './CreatePost';
import RoutePath from '../routes/RoutePath';
postRow(p){
return (
<tr key = {p.id}>
<td>{p.title}</td>
<td>{p.description}</td>
<td><Link to='/edit'></Link></td>
<td><Link to='/delete'></Link></td>
</tr>
)
}
render() {
const posts = this.state.posts.map(post => this.postRow(post));
return (
<div>
<table border="1">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{ posts }
</tbody>
</table>
<div>
<Link to='/add-post'>Add</Link>
</div>
</div>
);
}
}
So basically the flow is here:
app.js
to /public/js
in default.app.js
requires RoutePath
that contains all of your routing stuff.RoutePath
was trying to render all of your component you have in root ('/'
), and it render Example
'homepage'
id in your blade.Upvotes: 0