React调整更改组件的大小
React resize change components
我有一个React应用程序,其中有一个用于调整窗口对象大小事件的事件侦听器。
class App extends React.Component {
constructor(props){
super(props);
this.state = { width: 0 }
this.setSize = this.setSize.bind(this);
}
render() {
let content = this.state.width < 600 ? <MobileComponent/> : <DesktopComponent />;
return(<div>{content}</div>)
}
setSize() {
this.setState({
width: window.innerWidth
});
}
componentDidMount() {
this.setSize();
window.addEventListener('resize', this.setSize);
}
componentWillUnmount() {
window.removeEventListener('resize', this.setSize);
}
}
这是可行的,但我的问题是,我不知道如何访问子组件中应用程序组件的状态(窗口宽度)。
那么,有人能告诉我如何在我的孩子组件中访问/发送this.state.width吗?(我知道我可以用道具发送,但如果我的组件中还有两个组件呢?)
render() {
let content = this.state.width < 600 ? <MobileComponent/> : <DesktopComponent />;
return(<div>
<div>{content}</div>
<childComponent myWidth= {this.state.width}></childComponent >
<childComponent2 myWidth= {this.state.width}></childComponent2 >
</div>
)
}
相关文章:
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- 调整窗口大小时,可拖动的对象会出现在容器外部
- ExtJS——在展开/折叠时调整面板高度
- React组件等待所需道具进行渲染
- 如何创建带有插槽的vue js组件预加载程序
- KnockoutJS-组件-多个实例
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何在react js中将值从一个组件发送到另一个组件
- 宽度减小时调整React导航栏组件的大小
- React调整更改组件的大小
- HTML图像&其他组件根据页面宽度调整大小
- 组件开始重叠,然后在窗口大小调整时移动
- Reactjs可拖动和可调整大小的组件
- 调整浏览器大小时,html组件的比例大小
- 如何根据React中同级组件的大小调整组件的大小
- 如何根据检票口中的另一个组件调整面板宽度
- ExtJS 4 - 使用 resizeHandle 调整大小后,内部组件未在适合布局中调整大小