【发布时间】:2018-08-26 09:48:11
【问题描述】:
我正在关注 O'Reilly 的“Learning React”的第 5 章“React with JSX”。
我使用create-react-app 作为基础编写了食谱应用程序。
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import Menu from './Menu';
import registerServiceWorker from './registerServiceWorker';
import data from './data/recipes';
window.React = React;
ReactDOM.render(<Menu recipes={data} />, document.getElementById('root'));
registerServiceWorker();
Menu.js
import Recipes from './Recipes';
const Menu = ({recipes}) => (
<article>
<header>
<h1>Delicious Recipes</h1>
</header>
<div className = "recipes">
{recipes.map((recipe, i)=>
<Recipes key={i} {...recipe} />
)}
</div>
</article>
);
export default Menu;
并且出现以下错误:
Failed to compile ./src/Menu.js
Line 5: 'React' must be in scope when using JSX react/react-in-jsx-scope
Line 6: 'React' must be in scope when using JSX react/react-in-jsx-scope
Line 7: 'React' must be in scope when using JSX react/react-in-jsx-scope
Line 9: 'React' must be in scope when using JSX react/react-in-jsx-scope
Line 11: 'React' must be in scope when using JSX react/react-in-jsx-scope
Search for the keywords to learn more about each error.
This error occurred during the build time and cannot be dismissed.
这本书说“将window.React 设置为React 会在浏览器中全局公开React 库。这样所有对React.createElement 的调用都可以确保正常工作”。但似乎我仍然需要在每个使用 JSX 的文件上导入 React。
【问题讨论】:
标签: reactjs jsx create-react-app react-dom