創建一個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