2017-09-06 42 views
0

我創建了一個部件的包裝材料,並調用調度功能的子組件的無限循環渲染(改變Redux的狀態),但我不明白爲什麼這個組件必須被重新描繪,因爲道具不IMO改變。ReactJS +終極版:無限渲染(功能)部件的包裝材料+在子組件調度(州)環

打包機:

export default function(ComposedComponent){ 
    class Authenticate extends Component { 
     componentWillMount(){ 
      //If not logged in 
      if (!this.props.loggedIn){ 
       this.context.router.history.replace({ pathname: '/login' }); 
      } 
      console.log("Mount parent"); 
     } 
     render(){ 
      return(
        <ComposedComponent {...this.props} /> 
       ); 
     } 
    } 
    Authenticate.propTypes = { 
     loggedIn: PropTypes.bool.isRequired 
    } 
    // Provide router in context for redirect 
    Authenticate.contextTypes = { 
     router: PropTypes.object.isRequired 
    } 
    const mapStateToProps = state => ({ 
     loggedIn: state.user.loggedIn 
    }) 

    return connect(mapStateToProps)(Authenticate); 
} 

輔元件:

class InternalArea extends Component { 
    componentDidMount(){ 
     this.props.setTitle(this.props.title); 
     console.log("Mount child"); 
    } 

    render(){ 

     return(
      <div> 
       This is an internal area for logged in users. 
      </div> 
     ); 
    } 

} 
const mapDispatchToProps = dispatch => bindActionCreators({ 
     setTitle 
    }, dispatch) 

export default connect(
    null, 
    mapDispatchToProps 
)(InternalArea); 

包裝物integegrated作爲路由在App.js(PropsRoute只是傳遞額外道具):

<PropsRoute exact path="/internal-area" component={requireAuth(InternalArea)} title="Internal Area" /> 

卸下this.props.setTitle(this.props.title);在子組件解決了循環錯誤,但我需要派遣它療法即或者我應該移動它?爲什麼它改變道具?

回答

0

最後我想通了如何不創建這個循環不使用的功能,但包裝接收路徑組件作爲孩子App.js.包裝部件該方案建議在:https://www.cuttlesoft.com/infinitely-dispatching-actions-react-redux-react-router-v4/

儘管如此,我還是不能100%理解爲什麼創造了這個循環。我爲有點混亂,功能包裝仍然是重定向一個經常提出的方法,雖然它似乎沒有與調度的子組件的終極版行動的工作。