2016-09-26 46 views
2
import React,{ Component } from 'react'; 
import { connect } from 'react-redux'; 
import { itemid } from '../actions/action'; 


class DetailPage extends Component{ 
    componentWillUpdate(){ 
    this.props.dispatch(itemid([this.props.params.item,`${this.props.params.item}${this.props.params.id}`])) 
    } 
    render(){ 
     const { dispatch } = this.props; 
     return(
       <div> 
        {this.props.params.id} 
       </div> 
      ) 
    } 
} 
function selectstate(state){ 
    return{ 
     state 
    } 
} 
export default connect(selectstate)(DetailPage) 

// {} this.props.params.item是从反应路由器(路径( '/细节/项目/ ID'))最大调用堆栈大小超出反应

为什么我的调度是无限循环,直到 错误(超出最大调用堆栈大小)

回答

2

您正在发送调度componentWillUpdate。所以每次更新状态时,都会再次更新状态。你应该从未修改状态componentWillUpdate:https://facebook.github.io/react/docs/component-specs.html#updating-componentwillupdate

顺便说一句,在componentWillReceiveProps这样做很可能会做同样的事情,因为你的状态,从终极版店,将做为props

问题是你为什么要在更新后做调度?是否因为下面的功能设置状态?

如果您希望在设置特定状态后进行分派,则可以使用shouldComponentUpdate来阻止除该状态以外的更新。但是这在这种配置中感觉就像是一种副作用。我认为你在设置状态的同时调度事件要好得多,或者作为回调。

相关问题