我试图使用react显示一个简单的表单,但是我得到了错误“目标容器不是DOM元素”,尽管根元素肯定是DOM元素,因为我已经使用控制台进行了检查。
编辑:导入表单“react-dom”并使用“render”可以很好地工作,但我似乎不能让它工作
下面是表单组件
import React from 'react';
export default function StudentForm()
{
return (
<form>
//some stuff
</form>
)
}
我的索引文件
import ReactDOM from 'react-dom/client';
import './index.css';
import StudentForm from './App';
const root = document.getElementById('root');
ReactDOM.createRoot(<StudentForm/>,root);
下面是我的index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
3条答案
按热度按时间vbkedwbf1#
我猜您的index.html中没有id为
root
的元素,请在index.html中添加以下标记:<div id="root" />
gxwragnw2#
试试这个:
ss2ws0br3#
您需要将主脚本或索引脚本添加到HTML中。