我如何在 React 样板中使用 jsx 而不是 js 扩展?
How can i use jsx instead of js extension in react boilerplate?
我只想在 https://github.com/react-boilerplate/react-boilerplate/
中添加 jsx 页面
下面是我要添加密码忘记页面jsx文件内容。
import React from 'react';
import { connect } from 'react-redux';
import {Link, Route} from 'react-router-dom';
class PwdForgotPage extends React.Component {
render() {
return (
<div className="col-md-6 col-md-offset-3">
Password forgot.
<button className="btn btn-link">Back</button>
</div>
);
}
}
function mapStateToProps(state) {
const { registering } = state.registration;
return {
registering,
};
}
const connectedPwdForgotPage = connect(mapStateToProps)(PwdForgotPage);
export { connectedPwdForgotPage as PwdForgotPage };
但是如果我使用 jsx 扩展,则无法 运行 如下。
Module parse failed: C:\.....\frontend\app\containers\PwdForgotPage\PwdForgotPage.jsx Unexpected token (8:6)
You may need an appropriate loader to handle this file type.
| render() {
| return (
| <div className="col-md-6 col-md-offset-3">
| Password forgot.
| <button className="btn btn-link">Back</button>
如果我使用 js 扩展,它可以工作。
我如何更改样板中的配置以支持 jsx 扩展?
您需要在启用 babel babel-loader
.
的 webpack 配置中的规则中添加 jsx
扩展
{
test: /\.jsx?$/, // notice optional x?
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: options.babelQuery,
},
},
或者主观上更具可读性/\.(js|jsx)$/
从您现有的项目中,您可以 运行:
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
然后.jsx 文件将被解释。
有关详细信息,请查看以下站点:
https://create-react-app.dev/docs/adding-typescript/
我只想在 https://github.com/react-boilerplate/react-boilerplate/
中添加 jsx 页面下面是我要添加密码忘记页面jsx文件内容。
import React from 'react';
import { connect } from 'react-redux';
import {Link, Route} from 'react-router-dom';
class PwdForgotPage extends React.Component {
render() {
return (
<div className="col-md-6 col-md-offset-3">
Password forgot.
<button className="btn btn-link">Back</button>
</div>
);
}
}
function mapStateToProps(state) {
const { registering } = state.registration;
return {
registering,
};
}
const connectedPwdForgotPage = connect(mapStateToProps)(PwdForgotPage);
export { connectedPwdForgotPage as PwdForgotPage };
但是如果我使用 jsx 扩展,则无法 运行 如下。
Module parse failed: C:\.....\frontend\app\containers\PwdForgotPage\PwdForgotPage.jsx Unexpected token (8:6)
You may need an appropriate loader to handle this file type.
| render() {
| return (
| <div className="col-md-6 col-md-offset-3">
| Password forgot.
| <button className="btn btn-link">Back</button>
如果我使用 js 扩展,它可以工作。 我如何更改样板中的配置以支持 jsx 扩展?
您需要在启用 babel babel-loader
.
jsx
扩展
{
test: /\.jsx?$/, // notice optional x?
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: options.babelQuery,
},
},
或者主观上更具可读性/\.(js|jsx)$/
从您现有的项目中,您可以 运行:
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
然后.jsx 文件将被解释。
有关详细信息,请查看以下站点: https://create-react-app.dev/docs/adding-typescript/