【问题标题】:Why React Render element not fired为什么没有触发 React Render 元素
【发布时间】:2018-10-30 07:30:25
【问题描述】:

在我的编码中,我在 wwwroot 中有一个带有以下代码的 html 文件。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>sample1</title>
    <script src="/Sample.js"></script>
</head>
<body>
    <h2>Sample1</h2>
<div id="combo1"></div>
</body>
</html>

我的 Sample.js 文件包含以下内容

export class CommentBox extends React.Component {
    render() {
        return (
            <div className="commentBox">Hello, world! I am a CommentBox.</div>
            <h2>hey hello</h2>
        );
    }
}

ReactDOM.render(<CommentBox />, document.getElementById('combo1'));

但我没有得到渲染元素...

任何人都可以指导我..

提前感谢

【问题讨论】:

标签: reactjs .net-core


【解决方案1】:

render() 中的 jsx 存在问题

把它改成这样(即将h2标签移动到div内)

render() {
  return (
    <div className="commentBox">Hello, world! I am a CommentBox.
      <h2>hey hello</h2>
    </div>    
  );
}

【讨论】:

    【解决方案2】:

    DOM 可能还没有准备好。尝试将脚本移动到 &lt;/body&gt; 之前。

    我还假设您已经加载了 React 和 ReactDOM 包,并且正在使用 Babel 在渲染方法中处理 JSX。

    如果不看create react app。

    【讨论】:

    • 已经试过了兄弟。你能给任何带有本地静态数据的链接示例项目以在.netcore中使用reactjs加载组合
    • 我相信 .netcore 能够生成基于反应的项目。值得一看。
    猜你喜欢
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2021-05-23
    • 2021-06-21
    • 2019-02-19
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多