I recently discovered React and I'm trying to understand how it works.
I put this code:
class App extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h1>Hello World</h1>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("app"));
Super simple, but my page is blank instead of showing h1 element I suposedly rendered here.
Can someone explain why doesn't it work and what am I missing to make it work?
There is also a simple element in the HTML file along with all instructed code from the React doc page:
<div id="app"></div>
<script
src="https://unpkg.com/react#18/umd/react.development.js"
crossorigin
></script>
<script
src="https://unpkg.com/react-dom#18/umd/react-dom.development.js"
crossorigin
></script>
<script src="index.js"></script>
Stack Snippet:
class App extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h1>Hello World</h1>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("app"));
<div id="app"></div>
<script
src="https://unpkg.com/react#18/umd/react.development.js"
crossorigin
></script>
<script
src="https://unpkg.com/react-dom#18/umd/react-dom.development.js"
crossorigin
></script>
I suspect you don't have anything in place to handle JSX. Note that <App /> is invalid JavaScript syntax. It's a JavaScript extension called JSX. You don't have to use JSX with React, but most people do.
If you want to use JSX, you'll need to use Babel or similar to compile (aka "transpile") the JSX into calls to React.createElement. Usually you do that as a build step so that what's deployed is already transpiled (and minified and bundled). There is an in-browser way of doing it with Babel Standalone, but it's (strongly) not recommended for production. (This meta question shows using Babel standalone.)
Related
i need help for integrate frontend ReactJS on my site. currently i avoid to use nodeJS or NPM for implement my reactJS. but i'm using ReactJS CDN. so i not run npm start when develop the reactJS. i just want to use the frontend.
Hope this will understand from the start. so i have a question, how do i able to get another component and place it on single file.
I Created file App.js. I plan that this file will become a centralize for other components.
let me share what i'm doing right now.
here is my file structure
this is my code on index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Buynow Project</title>
<script src="https://unpkg.com/react#16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom#16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone#6/babel.min.js"></script>
<script type="text/babel" src="buynow/index.js"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
this is my code on index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js'
ReactDOM.render(<App />, document.getElementById('root'));
this is my code on App.js
import React from 'react';
import Navbar from './components/Navbar.js'
function App(props){
return (
<div>
<Navbar/>
Hello App
</div>
);
}
export default App;
this is my code on Navbar.js
import React from 'react';
function NavBar(props) {
return (
<div>
<Navbar/>
Hi NavBar
</div>
);
}
export default NavBar;
but i got this error, and pointed on file index.js line:1
Uncaught ReferenceError: require is not defined
this is error
I confuse how to solve this. I start with the simple code just for testing if it can work or not.
please help.
https://reactjs.org/docs/react-without-jsx.html
JSX is not a requirement for using React. Using React without JSX is
especially convenient when you don’t want to set up compilation in
your build environment.
So you can't use JSX/TSX directly in your browser (need compilation). Also, you can't use import outside a module (so basically now, you can already use your function components by adding the import of all your files in the index.html).
In my experience I suggest that you use the benefits of JSX / TSX and compile your code with NPM to be more comfortable (everything is way more intuitive with jsx).
In any case, in the link that I have included, you will find how to do the equivalent of JSX with pure javascript. That's what you need. (as you can see in my small snippet)
If you want keep using react from CDN and without compiling, your code, should be something like this:
// For the Snippet i have all of your js here
// But you could just import in your index.html every separate component file you have (without any import/export syntax)
function NavBar(props) {
return React.createElement('span', null,props.title);
}
function App(props){
return React.createElement('span', null, React.createElement(NavBar, {title: 'Header'}, null),
React.createElement('span', null,'Hello App'));
}
ReactDOM.render(React.createElement(App, {}, null), document.getElementById('root'));
<html>
<head>
<meta charset="UTF-8" />
<title>Buynow Project</title>
<script src="https://unpkg.com/react#16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom#16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone#6/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
Anyway, as you see, the code is not so readable this way...
EDIT
I made the same snippet on stackblitz with separated files
https://stackblitz.com/edit/web-platform-4ruju9?file=index.html
Ps. Also, don't put NavBar inside NavBar or you'll get a render loop
You have to enable module support in script tag
<script type="text/babel" data-type="module" src="./index.js"></script>
Remove React import statements as they can be directly used.
I am trying to render a component inside another component and then render that code into a html file. I tried everything I knew but can't get to work. I am new to React JS and want to learn it as plain as possible without any tooling and bundlers. Hence I am linking react, react-dom and babel in my html file and creating two different component js files. Can you please look at the code and guide what is wrong with it and how to fix it. I have tried export default before class Header and just export and in App.js file I tried import {Header} from './Header'; but still did not work. Thank you.
/* Header Component in a Header.js file */
class Header extends React.Component
{
render()
{
return(
<div>
<h1>My Beautiful Website</h1>
<hr />
</div>
);
}
}
export default Header;
/* App Component in App.js file */
import Header from './Header';
class App extends React.Component
{
render()
{
return (
<div>
<Header />
<section>
<h2>I am trying to make this React thing work</h2>
<p>
It should be easy to do I am trying to do but may be I am a little confused about
things in React JS. Sooner or later though everything gets easier and most importantly
crystal clear to the human mind when you persevere. I believe.
</p>
</section>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/0.14.8/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React Hello world</title>
</head>
<body>
<div id="root"></div>
<script src="./App.js" type="text/babel"></script>
</body>
</html>
You have to pass additional attribute transform-es2015-modules-umd to scripts if you want to use import,export and jsx
<script data-plugins="transform-es2015-modules-umd" src="./Header.js" type="text/babel"></script>
<script data-plugins="transform-es2015-modules-umd" src="./App.js" type="text/babel"></script>
Example
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.14.0/babel.min.js"></script>
<div id="root"></div>
<script type="text/babel" data-plugins="transform-es2015-modules-umd">
class Header extends React.Component
{
render()
{
return(
<div>
<h1>My Beautiful Website</h1>
<hr />
</div>
);
}
}
export default Header;
</script>
<script type="text/babel" data-plugins="transform-es2015-modules-umd">
// import Header from "./Header";
const Header = window.InlineBabelScript.default
class App extends React.Component
{
render()
{
return (
<div>
<Header/>
<section>
<h2>I am trying to make this React thing work</h2>
<p>
It should be easy to do I am trying to do but may be I am a little confused about
things in React JS. Sooner or later though everything gets easier and most importantly
crystal clear to the human mind when you persevere. I believe.
</p>
</section>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
</script>
I'm trying to get React.js to work client side on my LAMP stack web application. I want to implement the following slider on my website: https://github.com/airbnb/rheostat. I have managed to get react.js to work using the following guide: https://gist.github.com/jineshshah36/4a0326abb191781184e9.
The following code works:
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script type="text/babel">
var HelloComponent = React.createClass({
render: function() {
return (
<h1>Hello, World!</h1>
)
}
})
ReactDOM.render(<HelloComponent />, document.querySelector('#app'))
</script>
I tried to use the rheostat slider by changing the above to the following.
<div id="slider-root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script src="https://raw.githubusercontent.com/airbnb/rheostat/master/src/Slider.jsx"></script>
<link href="https://raw.githubusercontent.com/airbnb/rheostat/master/css/slider.css" rel="stylesheet" type="text/css">
<script type="text/babel">
import Rheostat from 'rheostat';
ReactDOM.render(<Rheostat />, document.getElementById('slider-root'));
</script>
Am I importing the files wrong? Do I need to process the jsx file before it can be executed by the browser? If yes, how do I do that?
Do I need to process the jsx file before it can be executed by the browser?
Yes, precisely. jsx is essentially a syntax extension primarily for working with React, and you will need to compile/transpile first. If you don't want to do that, you can simply leave jsx out altogether and just use:
ReactDOM.render(React.createElement(Rheostat), document.getElementById('slider-root'));
If you are interested in jsx, consider the following:
<div>
<span>Text here</span>
</div>
This is not valid JavaScript, the browser will barf up all sorts of syntax errors restlessly. This code first has to be compiled, or transpiled into valid JavaScript. When using React, the above is to be compiled into:
React.createElement(
"div",
null,
React.createElement(
"span",
null,
"Text here"
)
);
One of the most common tools for this is Babel. TypeScript also has built-in support for typed jsx, dubbed tsx.
I got starting ASP.NET MVC project with the following resources added:
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/bootstrap")
<script src="~/Scripts/require.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
#RenderSection("scripts", required: false)
Section scripts in Index.cshtml:
<div id="root"></div>
#section scripts
{
<script src="~/Scripts/ReactJS/Index.js" type="text/babel"></script>
}
And the file itself:
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);
The result is empty page with no errors.
I do not want to use Node.js, I do not want to use even require.js and babel.js, but it seems I have to.
What are the minimal dependencies of reactjs to work properly?
First, ensure ReactJS core files & Remarkable.js referenced properly as this:
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/[version]/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/[version]/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/remarkable/[version]/remarkable.min.js"></script>
(more info: ReactJS MVC / ReactJS Core MVC)
Then, add JSX file in either <head> tag or Scripts section:
#section Scripts {
<script src="~/Scripts/ReactJS/Index.jsx"></script>
}
Assume in your <body> tag has this div container to bind...
<div id="root"></div>
Then, in JSX file the React script should be looks like this:
// Index.jsx
var Root = React.createClass({
render: function () {
return (
<h1>Hello, world!</h1>
);
}
});
ReactDOM.render(<Root />, document.getElementById('root'));
As the tutorial page said, BabelJS required if you want to use ES6 features such as arrow functions & concise methods, or use bundling mechanism (BabelBundle instead of plain ScriptBundle) to load JSX file.
Related issue:
ReactJS.NET MVC tutorial doesn't work?
Please use div before h1
var Root = React.createClass({
render: function () {
return (<div>
<h1>Hello, world!</h1> </div>);
}
});
I'm trying to use React without webpack, node etc.
I want structure each component in separate file, this is what I'm doing:
index.html
<html>
<body>
<div id="root">
<!-- This div's content will be managed by React. -->
</div>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script src="https://unpkg.com/react#15/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom#15/dist/react-dom.min.js"></script>
<script src="https://unpkg.com/babel-standalone#6/babel.min.js"></script>
<script type="text/babel" src="js/demandAnalysis/app.js"></script>
<script type="text/babel">
ReactDOM.render(<App />, document.getElementById('root'));
</script>
</body>
</html>
App.js
import SimpleButton from './componets/Buttons/SimpleButton.js';
class App extends React.Component {
render() {
return (
<div>
<Button/>
</div>
);
}
}
SimpleButton.js
class SimpleButton extends React.Component {
render() {
return (
<div>
<button type="button">Click me!</button>
</div>
);
}
}
But give me this error in console:
Uncaught ReferenceError: require is not defined
I think is caused by the import, how can use this structure also without node or web pack?
import is an ES6 feature, which is currently not supported by many browsers. That is the reason why babel is used to transpile your code into ES5. This is also true for class, extends and JSX that you are using.
If you do not want to use a transpiler you should use require instead of import and React.createClass({ ... }) instead of class.
An example of require instead of import:
var SimpleButton = require('./componets/Buttons/SimpleButton.js');