Javascript 长条进口及;从react组件导出
我不能使用依赖项/包管理器或构建系统,但我想使用如下组件: 具体地说Javascript 长条进口及;从react组件导出,javascript,reactjs,Javascript,Reactjs,我不能使用依赖项/包管理器或构建系统,但我想使用如下组件: 具体地说 import PropTypes from 'prop-types'; // CLASS CODE export default Clusterize; 请注意,它使用“导入”和“导出”,尽管它是专门用于浏览器的代码。我知道这是非常常见的,许多项目使用构建系统来处理这一问题,但我不能 如何剥离导入/导出以使类进入全局范围。是否有一个库或cli可以做到这一点?您可以在浏览器中直接包含React、ReactDOM和Babel,并
import PropTypes from 'prop-types';
// CLASS CODE
export default Clusterize;
请注意,它使用“导入”和“导出”,尽管它是专门用于浏览器的代码。我知道这是非常常见的,许多项目使用构建系统来处理这一问题,但我不能
如何剥离导入/导出以使类进入全局范围。是否有一个库或cli可以做到这一点?您可以在浏览器中直接包含React、ReactDOM和Babel,并在不使用webpack或gulp的情况下使用它们。但是,对于生产环境,您需要包括这些脚本文件的最小化版本 要渲染任何内容,必须将rows prop传递给ClusterizeComponent,如:
<ClusterizeComponent rows={ data } />
这段代码应该给你一个好的开始
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id='app-root'></div>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://unpkg.com/react@15/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
<script src="https://cdn.bootcss.com/clusterize.js/0.17.6/clusterize.js"></script>
<script type="text/babel">
(() => {
class ClusterizeComponent extends React.Component {
static propTypes = {
rows: React.PropTypes.array,
scrollTop: React.PropTypes.number
};
static defaultProps = {
rows: [],
scrollTop: 0
};
clusterize = null;
scrollElem = null;
contentElem = null;
componentDidMount() {
const scrollElem = ReactDOM.findDOMNode(this.scrollElem);
const contentElem = ReactDOM.findDOMNode(this.contentElem);
const rows = this.props.rows.map(row => {
if (typeof row === 'string') {
return row;
}
return React.isValidElement(row) ? row : null;
});
this.clusterize = new Clusterize({
rows,
scrollElem,
contentElem,
show_no_data_row: false
});
}
componentWillUnmount() {
if (this.clusterize) {
this.clusterize.destroy(true);
this.clusterize = null;
}
}
shouldComponentUpdate(nextProps, nextState) {
return false;
}
componentWillReceiveProps(nextProps) {
if (nextProps.rows.length === 0) {
this.clusterize.clear();
return;
}
if (nextProps.rows !== this.props.rows) {
const rows = nextProps.rows.map(row => {
if (typeof row === 'string') {
return row;
}
return React.isValidElement(row) ? row : null;
});
this.clusterize && this.clusterize.update(rows);
}
if (nextProps.scrollTop !== this.props.scrollTop) {
this.scrollElem.scrollTop = nextProps.scrollTop;
}
}
render() {
const { className, style } = this.props;
return (
<div
ref={node => {
this.scrollElem = node;
}}
className={className}
style={{
height: '100%',
overflow: 'auto',
...style
}}
>
<div
ref={node => {
this.contentElem = node;
}}
/>
</div>
);
}
}
ReactDOM.render(
<ClusterizeComponent />,
document.getElementById('app-root')
);
})();
</script>
</body>
</html>
(() => {
类ClusterizeComponent扩展了React.Component{
静态类型={
行:React.PropTypes.array,
scrollTop:React.PropTypes.number
};
静态defaultProps={
行:[],
滚动顶部:0
};
clusterize=null;
scrollem=null;
contentElem=null;
componentDidMount(){
const scrollem=ReactDOM.findDOMNode(this.scrollem);
const contentElem=ReactDOM.findDOMNode(this.contentElem);
const rows=this.props.rows.map(row=>{
如果(行的类型=='string'){
返回行;
}
返回React.isValidElement(行)?行:空;
});
this.clusterize=新的clusterize({
排,
Scrollem,
contentElem,
显示\u无\u数据\u行:false
});
}
组件将卸载(){
if(this.clusterize){
这个。集群化。销毁(真);
this.clusterize=null;
}
}
shouldComponentUpdate(下一步,下一步状态){
返回false;
}
组件将接收道具(下一步){
if(nextrops.rows.length==0){
this.clusterize.clear();
返回;
}
if(nextrops.rows!==this.props.rows){
const rows=nextrops.rows.map(row=>{
如果(行的类型=='string'){
返回行;
}
返回React.isValidElement(行)?行:空;
});
this.clusterize&&this.clusterize.update(行);
}
if(nextrops.scrollTop!==this.props.scrollTop){
this.scrollem.scrollTop=nextrops.scrollTop;
}
}
render(){
const{className,style}=this.props;
返回(
{
this.scrollem=节点;
}}
className={className}
风格={{
高度:“100%”,
溢出:“自动”,
风格
}}
>
{
this.contentElem=节点;
}}
/>
);
}
}
ReactDOM.render(
,
document.getElementById('app-root')
);
})();
如果没有生成工具,您将无法使用导入/导出。你想要达到的最终目标是什么?您是否计划使用任何第三方软件包或仅在应用程序中构建的东西?我们有一个大型项目,其中代码库主要是浏览器组件。我们根据需要手动加载组件和其他库,以提高性能和seo。我们希望使用第三方库或组件,并且每次都需要fork来删除nodejs代码,这会给我们带来快速老化的技术债务。我们尝试了webpack,但是代码分块插件不够智能,无法知道用户已经下载了什么。它还将代码从全局命名空间中删除,因为我们在后端使用react rails进行渲染,这使得它成为一个不可接受的限制。使用此解决方案,导入和导出在包含来自npm的依赖项时仍然会抛出错误。保持版本控制很重要,不必在每个第三方组件中使用fork。