Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am having issues trying to render my React component within my 'login' view. I am using the Laravel Framework. The page is loaded, however I find no React component in my React Devtools. The Example component provided in the Laravel Project template renders fine. I could not find an error in my syntax, and I was hoping one of you could help me find my error.

My login.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel Login</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@200;600&display=swap" rel="stylesheet">
        <link href="{{ URL::asset('public/css/app.css') }}">
        
        
        
    </head>
    <body>
        <div id="login-form">

            
        </div>
        
        <script defer src='./js/app.js'> </script>
    </body>
</html>

My LoginForm.js

import React from 'react';
import ReactDOM from 'react-dom';


function LoginForm() {
    return (
        <div class= "container"> 
            Insert FORM here TEST
        </div>
    );
}

export default LoginForm;

if (document.getElementById('login-form')) {
    ReactDOM.render(<LoginForm />, document.getElementById('login-form'));
}

My web.php

<?php

use IlluminateSupportFacadesRoute;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/login', function () {
    return view('login');
});

My app.js

require('./bootstrap');


require('./components/Example');
require('./components/LoginForm');
question from:https://stackoverflow.com/questions/66056411/problem-trying-to-render-react-component-in-laravel-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
250 views
Welcome To Ask or Share your Answers For Others

1 Answer

I actually ended up solving this. I changed my LoginForm.js to look like this.

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


export default class LoginForm extends React.Component{
    render(){

    
    return (
        <div class= "container"> 
            Insert FORM here TEST
        </div>
    );
    }

}


if (document.getElementById('login-form')) {
    ReactDOM.render(<LoginForm />, document.getElementById('login-form'));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...