本文介绍了进口报表:有还是没有反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在有关无状态组件中是否需要导入 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 1import React, { PropTypes } from 'react';//OPTION 2import { 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 React.createElement(button,{onClick:action},Submit);Use option 1 because babel will transform your jsx<button onClick={action}>Submit</button>toReact.createElement("button", { onClick: action }, "Submit");所以你看到反应必须在范围之内。您有两个选项:So as you see react must be in scope. You have two options: import从'react'引发的反应; 或定义全局反应 这篇关于进口报表:有还是没有反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 10:37