【发布时间】:2018-05-04 16:44:05
【问题描述】:
我正在尝试使用 Lerna 和 React 构建概念验证。
这里是存储库:
https://github.com/SeanPlusPlus/lerna-react
到目前为止,如果你运行这个,上面的工作:
git clone git@github.com:SeanPlusPlus/lerna-react.git
cd lerna-react
lerna bootstrap
cd packages/app
yarn start
在packages/app/src/App.js我正在导入和渲染Headline组件(注意,我使用create-react-app创建这个目录):
import React, { Component } from 'react';
import logo from './logo.svg';
import Headline from 'headline';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
{ Headline }
</div>
);
}
}
export default App;
packages/headline/index.jsx 中的Headline 组件正在使用React.createElement 函数:
import React from 'react';
// const Headline = () => (
// <h1>Hello Lerna + React</h1>
// )
const Headline = React.createElement('div', null,
React.createElement('h1', null, 'Hello Lerna')
)
export default Headline
而且,如您所见,返回 JSX 的函数被注释掉了。
...现在...如果我更新此文件以返回 JSX:
import React from 'react';
const Headline = () => (
<h1>Hello Lerna + React</h1>
)
export default Headline
我的应用返回此错误:
Failed to compile.
../headline/index.jsx
Module parse failed: Unexpected token (4:2)
You may need an appropriate loader to handle this file type.
|
| const Headline = () => (
| <h1>Hello Lerna + React</h1>
| )
如何从我的Headline 组件中导出 JSX?
【问题讨论】:
-
会不会是编译器假设你试图在你的标题方法中返回 React? (Hello Lerna + React)也许尝试不同的消息。
-
是的,我想是的。必须编译它,然后导入编译后的代码。谢谢!
标签: javascript reactjs create-react-app lerna