我正在尝试遵循此链接中提供的代码示例:https://reactjs.org/docs/add-react-to-a-website.html#add-react-in-one-minute,以将react组件运行到现有的HTML页面中。
但是当我尝试将本地JavaScript
替换为JSX
时,出现此错误:
ReferenceError:未定义e
我知道我需要用一些东西代替e(LikeButton))
。但是这里没有提到要在渲染代码中更新什么。
likebutton.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.';
}
// Display a "Like" <button>
return (
<button onClick={() => this.setState({ liked: true })}>
Like
</button>
);
}
}
index.html:
<!-- 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>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- Load our React component. -->
<script src="{{ asset('theme_assets/js/react/likebutton.js') }}" type="text/babel"></script>
<script>
const domContainer = document.querySelector('#like_button_container');
ReactDOM.render(e(LikeButton), domContainer);
</script>
答案 0 :(得分:1)
替换
ReactDOM.render(e(LikeButton), domContainer);
使用
ReactDOM.render(<LikeButton />, domContainer);
此处的工作代码 https://jsbin.com/wuqoluyovu/edit?html,console,output