@mac
There are several ways to pass parameters from Laravel to React.js:
Example in Blade template:
1
|
<div id="root" data-props="{{ json_encode($data) }}"></div> |
Example in React component:
1 2 |
const data = document.getElementById('root').getAttribute('data-props'); ReactDOM.render(<App data={JSON.parse(data)} />, document.getElementById('root')); |
Example in React component:
1 2 3 4 5 |
componentDidMount() { fetch('/api/data') .then(response => response.json()) .then(data => this.setState({ data })); } |
Example in Laravel routes/web.php:
1
|
Route::get('/api/data', 'DataController@index'); |
Example in DataController.php:
1 2 3 4 |
public function index() { $data = // retrieve data from database or wherever return response()->json($data); } |
Example in React component:
1 2 3 4 5 |
componentDidMount() { fetch('/api/data') .then(response => response.json()) .then(data => this.setState({ data })); } |
These are just some of the ways you can pass parameters from Laravel to React.js. Choose the method that best fits your needs and requirements.