创建一个js文件并导出默认函数来帮助渲染反应组件。
像这样
import React from 'react';
import ReactDOM from 'react-dom';
export default function(component, container , props = {}, callback){
React.createElement(component , props);
ReactDOM.render(component, container, callback);
}
你的组件将看起来像这样
import React,{PropTypes} from 'react';
class MySampleComponent extends React.Component{
static propTypes ={
hide : PropTypes.bool
}
static defaultProps= {
hide : false
}
render(){
return(
<div style={display : this.props.hide : 'none' : 'block'}> </div>
);
}
}
导入上述功能呈现在js文件的组成部分,你将index.html中添加
import render from './pathto/RenderHelper'
import MyComponent from './MyComponent'
class IndexPage {
constructor(){
this.renderUI();
}
renderUI(){
var container = document.getElementById('react-component');
render(MyComponent, container, {hide : false});
}
}
请注意,您需要将您的page.index.js文件添加到webpack.config.js条目,以便webpack ca ñ编译它,像这样。
entry: { page.index : 'location/to/page.index.js' }
希望这会有所帮助。
'$ .hide()'和'$ .show()'来自jquery没有帮助吗? – flocks
可悲的是它不起作用。 $(“#react-component”)。hide()不起作用。 –
@VittVolt您是否尝试过使用style = {{display:'none'}}为父div? – Upasana