react爬坑之PureComponent下setstate不触发render中状态变化的问题与相关知识。

本文讨论了在React中PureComponent下setState不触发render的情况,分析了浅比较与深复制的原理,以及如何通过解构赋值、JSON深拷贝等方式解决这个问题。同时,解释了PureComponent与Component在shouldComponentUpdate上的区别,并建议在性能优化时谨慎使用PureComponent。
摘要由CSDN通过智能技术生成

react爬坑之PureComponent下setstate不触发render中状态变化的问题。

在学习react时出现情况: 明明改变了值, 但是就是不触发render。

下面贴代码

import React, { PureComponent } from 'react';
import CommentInput from '@/components/CommentInput';
import CommentList  from '@/components/CommentList';
import styles from './CommentApp.less';

class CommentApp extends PureComponent {
  
  constructor () {
    super();
    this.state = {
      comments:[]
    }
  }

  componentWillMount () {
    this._loadComments()
  }

  _loadComments () {
    let comments = localStorage.getItem('comments');
    if(comments){
      comments = JSON.parse(comments)
      this.setState({comments:comments})
    }
  }

  _saveComments (comments) {
    localStorage.setItem('comments',JSON.stringify(comments));
  }


  handleSubmitComment = (comment) =>{
   
    const {comments} = this.state;

    if(!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值