this is a simple question. but I tried from the laravel documentation. I ran php artisan preset react and then npm install && npm run dev, now I just want to write a simple react script
const title = React.createElement(
'h1',
{id:'title',className:'header'},
'Hello World'
)
ReactDOM.render(
title,
document.getElementById('react-container')
)
it doesn't work ( a Header with hello world should be added to the dom ) . but when I add react cdn it works ( header is added ) . do I still need to include the cdn for react after i installed it in laravel ? or am i doing something wrong . i searched the web. but its a bit complicated for me. I can't understand.
can someone please help me?
this is all my code in the laravel blade :
@extends('BaseLayouts.body')
@section('main_body')
<link rel="stylesheet" href="{{asset('css\app.css')}}" />
<script src="{{asset('js\app.js')}}"></script>
<script>
window.onload = function () {
const title = React.createElement(
'h1',
{id:'title',className:'header'},
'Hello World'
)
ReactDOM.render(
title,
document.getElementById('react-container')
)
};
</script>
<div id="react-container"></div>
@endsection
also i saw in a tutorial that a sample file written in react is added to js folder , in my casing there is no such file .
when I write the script in a separate file, phpStorm detects the React, but for React.createElement the createElement part phpStorm says unresolved function or method
it doesn't work. Have you included the correct javascripts in your view? Did you add all the imports in your javascript code?app.jsand get transpiled. That way you can do things likeimport * as React from 'react'to get react transpiled and included in your bundle.