Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React不替换div中的内容_Javascript_Reactjs - Fatal编程技术网

Javascript React不替换div中的内容

Javascript React不替换div中的内容,javascript,reactjs,Javascript,Reactjs,我正在试用基本的react应用程序,并尝试运行react。index.ejs中有一个保存内容的div标记。最初,我在server.js文件中将内容设置为“…”。当我添加react代码时,在源文件夹中的index.js文件中,div标记中显示的值应该更改,但不会更改。请帮忙 server.js: import config from './config'; import apiRouter from './api'; import express from 'express'; import fs

我正在试用基本的react应用程序,并尝试运行react。index.ejs中有一个保存内容的div标记。最初,我在server.js文件中将内容设置为“…”。当我添加react代码时,在源文件夹中的index.js文件中,div标记中显示的值应该更改,但不会更改。请帮忙

server.js:

import config from './config';
import apiRouter from './api';
import express from 'express';
import fs from 'fs'
const server = express();

server.set('view engine','ejs');

server.get('/',(req,res) => {
      res.render('index', {
    content: '....'
});
});
server.use('/api', apiRouter);
server.use(express.static('public'));

server.listen(config.port,() => {
    console.info('Express listening on port', config.port)
});
index.ejs:

<%- include('header') -%>
<div id ="root"><%- content -%></div>
<%- include('footer') -%>

正确的是document.getElementById('root')

如果仍然不起作用,请检查页面(bundle.js)中是否加载了
index.js
和'react'/'react-dom'lib文件


另外,请始终在浏览器的开发者工具中检查控制台,查看前端是否有错误。

是否使用将应用程序渲染为“root”?是的,在我的帖子中添加了react代码,将其更改为document.getElementById(“root”),并检查了index.js文件。我正在加载lib文件。
<!doctype html>
<html>
<head>
    <title>Hello EJS+</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
</head>
</html>
<script type="/bundle.js" charset="utf-8"></script>
</body>
</html>
import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
    React.createElement('h2',null,'Hello React'), 
    document.getElementbyId('root') 
 );