进口报表:有还是没有反应? [英] Import statements: with or without React?
问题描述
在有关无状态组件中是否需要导入 React
的讨论中有一点辩论,我找不到任何文档。所以:
There's a little bit of a debate at work on whether it is necessary to import React
in stateless components and I can't find any docs about it. So:
//OPTION 1
import React, { PropTypes } from 'react';
//OPTION 2
import { PropTypes } from 'react';
export const Button = ({ action }) => {
return (
<button onClick={action}>Submit</button>
);
}
Button.propTypes = {
action: PropTypes.func.isRequired,
};
有些人说选项1是使用JSX时的最佳做法;一些其他的思考组件将会失败,选项2。
Some people say Option 1 is the best practice when using JSX; some other think component will fail with Option 2.
我已尝试过两次,我看不到任何差异,该组件仍然可以在两种情况下工作。
I have tried both and I can't see any difference, the component still works in both cases.
选项1或选项2:哪一个是正确的?
Option 1 or Option 2: which one is correct?
推荐答案
使用选项1因为babel会转换你的jsx
< button onClick = {action}>提交< / button>
to < br>
React.createElement(button,{onClick:action},Submit);
Use option 1 because babel will transform your jsx
<button onClick={action}>Submit</button>
to
React.createElement("button", { onClick: action }, "Submit");
所以你看到反应必须在范围之内。您有两个选项:
So as you see react must be in scope. You have two options:
- import从'react'引发的反应;
- 或定义全局反应
这篇关于进口报表:有还是没有反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!