Javascript 为什么我的组件在道具更改时重新渲染?
每当Javascript 为什么我的组件在道具更改时重新渲染?,javascript,reactjs,react-native,redux,Javascript,Reactjs,React Native,Redux,每当animationType或animationComplete的值更改时,飞溅组件将重新渲染。我不明白为什么当没有道具进入渲染时它会重新渲染 export class Splash extends React.Component { static propTypes = { animationType: allowNullPropType(PropTypes.string), animationComplete: PropTypes.bool,
animationType
或animationComplete
的值更改时,飞溅组件将重新渲染。我不明白为什么当没有道具进入渲染时它会重新渲染
export class Splash extends React.Component {
static propTypes = {
animationType: allowNullPropType(PropTypes.string),
animationComplete: PropTypes.bool,
changeAnimation: PropTypes.func
};
componentDidMount() {
const {changeAnimation} = this.props;
// ...
}
componentDidUpdate() {
const {animationType, animationComplete} = this.props;
const shouldChangeScreen = (animationType === 'out' && animationComplete);
if (shouldChangeScreen) {
const {navigation} = this.props;
// ...
}
}
render() {
return (
<Fade id='splash' styles={styles.container}>
<Logo height='125' width='125'/>
</Fade>
);
}
}
const mapStateToProps = (state) => {
return {
animationType: state.fade.getIn(['splash', 'type']),
animationComplete: state.fade.getIn(['splash', 'complete'])
}
};
const mapDispatchToProps = (dispatch) => {
return {
changeAnimation: (id, type) => dispatch(changeAnimation(id, type))
}
};
export default connect(mapStateToProps, mapDispatchToProps)(Splash);
但这似乎不是一个很好的解决方案。这就是react的工作原理。道具的任何更改都会导致组件再次调用render方法,除非您使用shouldComponentUpdate阻止它。react就是这样工作的。道具的任何更改都会导致组件再次调用Rebug方法,除非您用SuffDebug更新来防止它。
您应该始终认为“Read”是指“渲染虚拟DOM”的反应。实际的DOM部分只有在真正发生更改时才会重新呈现。所以不必担心大量渲染。
你应该始终认为“渲染”是指“渲染虚拟DOM”的反应。实际的DOM部分只有在真正发生更改时才会重新呈现。因此,无需担心大量渲染。shouldcomponentupdate()是一种常见的解决方案。shouldcomponentupdate()是一种常见的解决方案。更具体地说,当父组件重新渲染时,其所有子组件也将重新渲染,即使传递给子组件的道具与上次完全相同。React的默认行为是沿树递归地重新渲染所有组件。更具体地说,当父组件重新渲染时,其所有子组件也将重新渲染,即使传递给子组件的道具与上次完全相同。React的默认行为是沿着树递归地重新渲染所有组件。shouldComponentUpdate() {
return false;
}