窗口.使用React和redux的innerHeight
window.innerHeight with React and redux
我最近问了一个问题(简单的窗口调整大小反应),以获得这个解决方案的大部分工作,但是现在我正在插入redux,以便我可以重构出web服务调用到一个动作创建者,并通过一个reducer接收数据。(调用是使用json -promise和我使用适当的redux中间件。)
但是现在我似乎不能使用窗口。innerHeight总是返回200:/
的问题是由于有一些状态管理的redux和一些本地的组件(即。"高度")?
提前感谢!
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import GalleryItem from '../components/gallery_item';
import Nav from '../components/nav';
import { fetchImages } from '../actions/index';
export const MF = 'http://www.veilcraft.com';
class Gallery extends Component {
constructor(props) {
super(props);
this.state = {
images: [],
height: window.innerHeight
};
this.handleResize = this.handleResize.bind(this);
this.props.fetchImages();
}
componentDidMount() {
window.addEventListener('resize', this.handleResize);
}
handleResize(e) {
this.setState({
height: window.innerHeight
});
}
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
}
render() {
console.log(this.props.height); //outputs 200!
const galleryItems = this.props.images.map((image) => {
return <GalleryItem key={image} image={image} height={this.props.height} />
});
return (
<div>
<Nav />
<section>
{galleryItems}
</section>
</div>
);
}
}
function mapStateToProps(state) {
return {
images: state.images
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ fetchImages }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(Gallery);
行动:
import jsonp from 'jsonp-promise';
import { MF } from '../containers/gallery'
export const FETCH_IMAGES = 'FETCH_IMAGES';
export function fetchImages() {
const url = `${MF}/home/`
const request = jsonp(url, {}).promise;
return {
type: FETCH_IMAGES,
payload: request //return promise
};
}
减速机:
import { FETCH_IMAGES } from '../actions/index'
export default function(state = [], action) {
switch(action.type) {
case FETCH_IMAGES:
var images = action.payload.data.images.images.data.map((image) => {
return image.image;
});
return [ ...images, ...state ];
}
return state;
}
在handleResize
中,您正在更新Gallery
组件的状态。然后将height
传递到GalleryItem
作为支柱。
这意味着,在Gallery中,你需要写
console.log(this.state.height);
在GalleryItem中你可以写:
console.log(this.props.height);
相关文章:
- React Redux错误页面管理
- 在Redux中,我应该在哪里编写复杂的异步流
- TypeError:_this.store.getState在使用来自Redux的连接时不是函数
- React redux初始化功能,无论状态变化如何
- 为什么react/redux前端项目在package.json中包含生产依赖项
- 设计redux状态树
- React+Redux性能优化与组件ShouldUpdate
- Redux应用程序结构-在哪里放置服务/业务逻辑
- 在达到状态之前在Redux Reducer中设置日期格式
- React Redux无法让减速器拾取动作
- reducers在redux中得到Function not Object,what'it’他错了
- Redux处理新项目和id
- Redux中间件在next()之前更改状态
- WebView返回错误的window.innerHeight
- 使用immutable.js在Redux中操作嵌套数组中的数据
- 使用Redux和React添加焦点更改类
- 使用react router/react router redux将特定的redux操作绑定到一个路由
- 在Redux中链接async和sync操作以计算初始状态
- 类叶组件中的connect()是react+redux中反模式的标志吗
- 窗口.使用React和redux的innerHeight