score:0
You can do it like this..
@extends('layouts.app')
@section('script')
<script type="text/javascript">
ReactDOM.render(<Example name="{{ $user->name }}"/>, document.getElementById('example'));
</script>
@endsection
@section('content')
<div id="example"></div>
@endsection
But better solution would be AJAX request to some controller that returns json data.
Best solution for you will this laravel package laracasts/PHP-Vars-To-Js-Transformer
score:7
This is perhaps a slightly cleaner solution than the others suggested. We don't need to specify each prop we pass in using this method either.
In your blade file:
<div id="react-component" data-foo='{{ $foo }}' data-bar='{{ $bar }}'>
On a side note, if $foo
is something that isn't JSON, e.g a Laravel Collection or an array, we can apply an additional method: $foo->toJson()
.
In your component:
if (document.getElementById('react-component')) {
const component = document.getElementById('react-component');
const props = Object.assign({}, component.dataset);
ReactDOM.render(<ReactComponent {...props} />, component);
}
You'll then have access to all the props you pass in using the data attribute from your html. Just like any other react you can access your props inside of your component like: this.props.foo
score:10
I do it like this eg. in index.blade.php
<div id="main" data-user_name="{{$user->name}}" />
Then in Main.js
const Main = (props) => {
return (
<div>
<Component {...props} />
</div>
);
}
if(document.getElementById('main')) {
const el = document.getElementById('main')
const props = Object.assign({}, el.dataset)
ReactDOM.render(<Main {...props} />, el);
}
Source: stackoverflow.com
Related Query
- Laravel 5.5 render a React component in a blade view with data from the controller
- How to render the React component with dynamic data realtime from socket.io high efficiency
- What is the preferred way to pass data from a Rails view to a react component in Rails 6?
- React Redux, load data from the API on first page load ONLY. Render component once data is loaded
- loop through multiple objects inside the first array from JSON data displaying two arrays with objects in React using FUNCTION COMPONENT
- How to render React Component with data from Redux store
- How to load a page with the data from a component in react
- Trying to render a React component from a Backbone View produces an error with no information
- Is there a right way to pass data into a React component from the HTML page?
- How to get the data from React Context Consumer outside the render
- How to pass the match when using render in Route component from react router (v4)
- Why is my React component render called twice, once without data and then later with data, but too late exception?
- How to pass data from React Form -> Flask Backend -> React Component (does it have something to do with CORS)?
- React router - pass api data to the linked component to open with a new page
- How can I use react useContext , to show data from context, in the same component
- Display data in a React component from an object where the keys are different but the values are the same
- Why React still render the child component with Error Boundary
- How to Read image data from API and render in React Component
- Render data from localStorage with react
- React Router "Link to" does not load new data when called from inside the same component
- Reading data from Firebase 3 into a React component with ES6 syntax
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- React router dom passing data from parent component to child router component does not pass the props.match
- React collect child component data on some event from the parent component
- render data in component which getting from react redux?
- Passing data from an Asp.Net view into a React Component
- How to pass data from React client into the Oauth2.0 Google strategy flow with passport.js
- How can I get the right data from database with react
- Use react component as a child or from within the render function of a React component
- React HOC with async call render component before the logic
More Query from same tag
- Gantt editable chart for ReactJs
- getting error when exporting variable in reactjs
- How to implement FreshworksWidget into next.js project?
- Get redux state on window.scroll
- Set new intiial SlateJS editor value with React onEffect
- how to use functional mobX store in react class components?
- how to translate string in a component using react-i18next?
- How to get the updated state value in intersection observer React?
- Apply changing markers array to Google map in react
- Jest issue - ReactElement.js: Failed to get mock metadata
- React setState with arrays , how to auto create an array id without construct
- How to change format from string date to utc
- React App ignores NODE_ENV of the production mode
- Update data in form. Stay editable. (React with Redux, sans redux-form)
- HOC component - array return true or false
- fetch API not sending data via post
- unable to set react-select-async-paginate initial value
- Firestore - Listen all deletions without downloading the entire collection (receiving "modified" events as "added")
- React + Redux: TypeError: Cannot read property 'push' of undefined
- Rendering a nested navigation in React
- Why data isn't updating after sorting in React Hooks?
- Using useState-hook to alter styled-components from within
- Change props value outsite react and reload component
- Component starts to break after some time - react
- TypeError: Cannot read property 'cases' of undefined
- React checkbox. DRY out onChange function to handle all checkboxes
- How manage global state using context API in React js
- React Js: export problems '(possible exports: default)'
- React / Typescript: "Property 'children' is missing in type" in imported library
- react duplicate key mechanism