栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

React:更新子组件而不渲染父组件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

React:更新子组件而不渲染父组件

您不可能想要什么。要将prop传递给子组件,父组件的状态或prop应该以某种方式更改。如您所知,这显然会触发重新渲染,因此所有子级都将重新渲染。要进行更新,

Clock
在这种情况下应重新渲染和卸载/重新安装您的组件以反映DOM更改。

如果您的应用程序不是很大,并且没有那么多的子代,则不必担心此问题,因为渲染并不那么昂贵。昂贵的一个是组件的DOM操作。在这里,React会扩散实际和虚拟DOM,

Label
即使重新渲染也不会卸载/重新安装组件。但是,如果您将
Label
组件编写为
PureComponent
,则不会重新渲染。但是要
Clock
像这样更新组件,是没有办法的。

class Label extends React.PureComponent {  render() {    console.log("rendered");    return (<p>{this.props.text}</p>)  }}const Clock = ({ date }) => (  <div>{date.toLocaleTimeString()}</div>)class App extends React.Component {  constructor() {    super()    this.state = {      date: new Date()    }  }  componentWillMount() {    this.interval = setInterval(      () => this.setState({ date: new Date() }),      1000    )  }  componentWillUnmount() {    clearInterval(this.interval)  }  updateTime() {  }  render() {    return (      <div>        <Label text="The current time is:" />        <Clock date={this.state.date} />      </div>    )  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366715.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号