简体   繁体   English

Laravel 5.5使用来自控制器的数据在刀片视图中呈现React组件

[英]Laravel 5.5 render a React component in a blade view with data from the controller

My goals is to be able to do something like this in my blade view. 我的目标是能够在我的刀片视图中执行类似的操作。

<Example name="{{ $user->name }}" />

But this doesn't render anything. 但这并没有呈现任何东西。

When I do: <div id="example"></div> it renders the component without the props value which is normal. 当我这样做: <div id="example"></div>它渲染没有props值的组件是正常的。

So I am trying to pass data that came from my controller to my React component as a prop. 所以我试图将来自我的控制器的数据作为道具传递给我的React组件。

I am not sure if it's even possible. 我不确定它是否可能。

This is my App.js: 这是我的App.js:

require('./components/Example');

This is my Example.js component: 这是我的Example.js组件:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class Example extends Component {
    render() {
        return (
            <div className="container">
                <div className="row">
                    <div className="col-md-8 col-md-offset-2">
                        <div className="panel panel-default">
                            <div className="panel-heading">Example</div>

                            <div className="panel-body">
                                Hello, {this.props.name}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

if (document.getElementById('example')) {
    ReactDOM.render(<Example />, document.getElementById('example'));
}

I do it like this eg. 我是这样做的,例如。 in index.blade.php 在index.blade.php中

<div id="main" data-user_name="{{$user->name}}" />

Then in Main.js 然后在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);
}

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, eg a Laravel Collection or an array, we can apply an additional method: $foo->toJson() . 另外,如果$foo不是JSON,例如Laravel Collection或数组,我们可以应用另一种方法: $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. 然后,您可以使用html中的data属性访问您传入的所有道具。 Just like any other react you can access your props inside of your component like: this.props.foo 就像其他任何反应一样,您可以访问组件内的道具,例如: this.props.foo

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. 但更好的解决方案是向某些返回json数据的控制器发出AJAX请求。

Best solution for you will this laravel package laracasts/PHP-Vars-To-Js-Transformer 这个laravel包laracasts / PHP-Vars-To-Js-Transformer将为您提供最佳解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM