I am new to React(Angular background) and trying to do client side rendering with react.
Following index.html which is sent by express application:
<html>
<head>
<title>My Web</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.1/react.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.1/react-dom.min.js"></script>
<script type="text/javascript" src="/static/src/js/firstComponent.js"></script>
</head>
<body id="mainBody">
</body>
</html>
Here is the react component file(firstComponent.js):
var FirstComponent = React.createClass({
'render': function(){
return <h1> Hello</h1>;
}
});
ReactDOM.render(<FirstComponent />, document.getElementById('mainBody'));
But on loading the page, react does not seems to work. What is wrong here?
4
Answers
Finally got things working. Following are the steps that I took:
HTML:
Component:
You have several problems. The ones that jump out at me are:
type="text/js"
is not one of the recognised MIME types for JS so the browser will ignore that script elementfirstComponent.js
is a JSX file, which isn’t supported by browsers, you need to transpile it into JavaScriptJust simple like that
There is some tutorials here Tutorial
CodePen exemple
Today there is this solution. (official ? )
Tutorial
FILE1
FILE2
Update React 18.
https://reactjs.org/docs/add-react-to-a-website.html ;
https://gist.github.com/gaearon/6668a1f6986742109c00a581ce704605/archive/87f0b6f34238595b44308acfb86df6ea43669c08.zip ;
https://gist.github.com/gaearon/6668a1f6986742109c00a581ce704605 ;