直接在浏览器运行jsx及高版本的js代码

时间:2024-04-24 12:38:19
<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type='text/javascript' src="./dist/React.js"></script>
<style>
.aaa {
width: 200px;
height: 200px;
background: red;
}
</style>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
class A extends React.Component {
constructor() {
super()
this.state = {
aaa: 111
}
}
click() {
console.log('click')
this.setState({
aaa: this.state.aaa + 1
})
}
componentDidMount() {
console.log('componentDidMount')
this.click()
}
componentDidUpdate() {
console.log('componentDidUpdate')
}
render() {
return React.createElement('div', {
id: 'id',
style: {
height: this.state.aaa
},
onClick: this.click.bind(this),
className: 'aaa'
}, this.state.aaa)
}
}
var a
window.onload = function () {
a = React.render(<A />, document.body)
console.log(a)
}
</script>
</head> <body>
<div>这个默认会被清掉</div> </body> </html>

你如果将babel改成国内的

    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

变成

<script src="https://cdn.bootcss.com/babel-standalone/6.24.0/babel.js"></script>

什么react, react-dom, redux, react-redux都可以在 http://www.bootcdn.cn/ 上找到