<div id="app"></div>
<Toast id="toast" />
;
(https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy#readme)
webpack1.x 配置=> webpack.config.js
...
module:{
loaders:[
{
test:/.js[x]?$/,
exclude: /node_modules/,
loader:'babel-loader',
query:{
presets:['es2015','react','stage-0'],
plugins:['transform-runtime','transform-decorators-legacy']
}
}
]
}
1、注册组件(相关)
import React from 'react';
import { ref } from 'react.eval';
@ref
class Toast extends React.Component {
constructor(props) {
super(props);
this.state={
show:false,
};
}
show=()=>{
this.setState({
show:true,
});
}
render() {
const { show } = this.state;
return show && (
<div>Hello world!!!</div>
);
}
}
export default Toast;
2、引用组件(相关)
...
<HomePage/>
<Toast id="toast" />
...
3、任意位置调用组件实例(更多方式)
import { refs } from 'react.eval';
...
somewhere(){
refs.toast.show();
}
...