对象实例上的引用-React
Reference on object instance - React
我正在使用react来渲染传单地图。由于我无法从DOM中获取映射对象实例,因此如何从不同路由内的组件中检索实例?
App.js
const Main = React.createClass({
componentDidMount: function() {
map = L.map('map', {}).setView([45, 0], min_zoom);
let layer = L.tileLayer('https://api....}', { }).addTo(map);
},
render() {
return (
<div id='map'>
<div id='container'>
{this.props.children}
</div>
</div>
);
}
});
import Travel from './routes/Travel';
render((
<Router history={history}>
<Route path="/" component={Main}>
<Route path="/travel/:name" component={Travel}/>
</Route>
</Router>
), document.getElementById('app'));
路线/旅行.js
const Travel = React.createClass({
componentDidMount: function() {
GET THE MAP INSTANCE HERE // UNDEFINED
},
render() {
return (
<div id='storyBox'>
</div>
);
});
module.exports = Travel;
非常感谢
您可以将Main中的映射实例作为属性传递给子组件:
const Main = React.createClass({
componentDidMount: function() {
this.map = L.map('map', {}).setView([45, 0], min_zoom);
let layer = L.tileLayer('https://api....}', { }).addTo(map);
},
render() {
return (
<div id='map'>
<div id='container'>
{React.cloneElement(this.props.children, {map: this.map})}
</div>
</div>
);
}
});
路线/旅行.js
const Travel = React.createClass({
componentDidMount: function() {
if (this.props.map) {
// whatever
}
},
render() {
if (!this.props.map) return null;
return (
<div id='storyBox'>
</div>
);
});
module.exports = Travel;
相关文章:
- 引用对象中的通用值
- React如何调用ES6类的呈现函数,使“this”不引用类本身
- 了解如何引用 react 组件中的属性
- 如何处理 React 0.12+ 中的组件引用变量和 props
- 在 React.js 中映射时引用道具时遇到问题
- 如何在React Component NPM模块中引用图像
- React:新安装的子组件无法访问父组件引用
- 对象实例上的引用-React
- 未呈现React引用子组件
- 如何在React上获得动态引用
- 当引用对象改变时,react .js视图不更新
- 在React中引用组件外部的变量
- 使用react时发生getElementById引用错误
- 当在html中引用jsx文件时,React渲染方法不工作
- 如何引用文档.在React组件中工作
- 如何从泛型javascript中引用React类
- 在一个具有规范化状态的React / Redux应用中,如何以及在哪里解析对其他实体的引用
- 引用对象实例的React.js模式
- 访问React中同级组件的引用
- 如何获得'子控制器'React native中NavigatorIOS的引用