以下是我的组件类。组件似乎永远不会执行componentWillUpdate(),即使我可以通过在mapStateToProps中的返回之前记录来查看状态更新。状态是100%更改,但组件不刷新。当商店状态发生变化时,React组件不会更新
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { search } from './mapActions'
import L from 'leaflet'
class Map extends Component {
componentDidMount() {
L.Icon.Default.imagePath = './images'
this.map = new L.Map('map', {
center: new L.LatLng(this.props.lat, this.props.lng),
zoom: this.props.zoom,
layers: L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '<a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
})
})
}
componentWillUpdate() {
console.log('UPDATE MAP')
L.geoJson(this.props.data).addTo(this.map)
}
render() {
return <div id="map"></div>
}
}
const mapStateToProps = (state) => {
return {
isFetching: state.isFetching,
data: state.data
}
}
const mapDispatchToProps = (dispatch) => {
return {
search: (name) => {
dispatch(search(name))
}
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Map)
这里是地图减速机:
const initialState = {
isFetching: false,
data: {}
}
export const map = (state = initialState, action) => {
switch(action.type) {
case 'REQUEST_SEARCH_RESULTS':
return Object.assign({}, state, {
isFetching: true
})
case 'RECEIVE_SEARCH_RESULTS':
return Object.assign({}, state, {
isFetching: false,
data: action.data
})
default:
return state
}
}
经过一些测试和记录看来,当它进入状态映射到的道具,它使用映射到道具的状态对象包含正确的数据,所以state.map.data是正确的,我可以看到从提取返回。但是,当我然后在componentWillUpdate()中记录this.props时,数据对象在那里但是为空。
你能告诉你的减速为好 ?这可能是因为你改变了状态而不是发送新的对象,所以Redux认为没有理由更新。 – Mijamo
没有什么'render':'
' –@DavinTryon我正在使用Leaflet来处理它自己的渲染,但是它仍然应该像更新组件一样。 –