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);
在子組件解決了循環錯誤,但我需要派遣它療法即或者我應該移動它?爲什麼它改變道具?