[英]Client side rendering React
我是 React(Angular 背景)的新手,并尝试使用 React 进行客户端渲染。 以下是由快递应用程序发送的 index.html:
<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>
这是反应组件文件(firstComponent.js):
var FirstComponent = React.createClass({
'render': function(){
return <h1> Hello</h1>;
}
});
ReactDOM.render(<FirstComponent />, document.getElementById('mainBody'));
但是在加载页面时,react 似乎不起作用。 这里有什么问题?
你有几个问题。 跳到我身上的是:
type="text/js"
不是JS公认的MIME类型之一,因此浏览器将忽略该脚本元素 firstComponent.js
是一个JSX文件,浏览器不支持,您需要将其转换为JavaScript 终于让事情起作用了。 以下是我采取的步骤:
HTML:
<html>
<head>
<title>My</title>
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="/static/src/webpack/bundle.js"></script>
</body>
</html>
零件:
import React from 'react'
import ReactDOM from 'react-dom'
class FirstComponent extends React.Component{
render(){
return (
<h1> Hello</h1>
);
}
}
if(typeof window !== 'undefined')
ReactDOM.render(<FirstComponent />, document.getElementById('root'));
今天有这个解决方案。 (官方的 ? )
1. https://ru.react.js.org/docs/add-react-to-a-website.html#add-react-in-one-minute.
2. https://gist.github.com/gaearon/6668a1f6986742109c00a581ce704605/archive/f6c882b6ae18bde42dcf6fdb751aae93495a2275.zip
教程
1. Inside your local hard disk create a folder, say /mnt/disk1/app01
2. Inside it create 2 files: index.html and like_button.js
3. Write the following code.
4. Open the file html with Chromium
文件1
<!-- FILE /mnt/disk1/app01/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Add React in One Minute</title>
</head>
<body>
<h2>Add React in One Minute</h2>
<p>This page demonstrates using React with no build tooling.</p>
<p>React is loaded as a script tag.</p>
<!-- We will put our React component inside this div. -->
<div id="like_button_container"></div>
<!-- Load React. -->
<!-- Note: when deploying, replace "development.js" with "production.min.js". -->
<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>
<!-- Load our React component. -->
<script src="like_button.js"></script>
</body>
</html>
文件2
//FILE /mnt/disk1/app01/like_button.js
'use strict';
const e = React.createElement;
class LikeButton extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
render() {
if (this.state.liked) {
return 'You liked this.';
}
return e(
'button',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
}
}
const domContainer = document.querySelector('#like_button_container');
ReactDOM.render(e(LikeButton), domContainer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.