我正在學習React,並且當我的代碼在我的客戶端中沒有導入「React」時無法工作時,我很困惑。 JS。理想情況下,當我在代碼中不使用「React」時,我不應該強制導入「react」模塊。以下是代碼片段。爲什麼即使我的client.js沒有使用它,也必須導入「react」
Layout.js:
import React from "react";
export default class Layout extends React.Component {
constructor() {
super();
this.name = "Dilip";
}
render() {
return (
<h1>Welcome {this.name} in React world !!</h1>
)
}
}
工作代碼:
import React from "react";
import ReactDOM from "react-dom";
import Layout from "./components/Layout"
const app = document.getElementById('app');
ReactDOM.render(<Layout/>, app);
不工作代碼:
import ReactDOM from "react-dom";
import Layout from "./components/Layout"
const app = document.getElementById('app');
ReactDOM.render(<Layout/>, app);
爲什麼當我刪除代碼導入它不起作用「響應「?我沒有在任何地方使用「React」,因此它應該可以工作。它在控制檯中引發以下錯誤。
Uncaught ReferenceError: React is not defined
注:我下面video
ReactDOM取決於反應 – Matteo
使用ReactDOM React – Yon
據我瞭解,但當我使用「ReactDOM」時,它對新用戶不可見。如果它取決於「反應」,那麼它應該作爲參數。 – joy