react怎么更新state

发布时间:2022-11-09 15:48:19 作者:iii
来源:亿速云 阅读:116

本文小编为大家详细介绍“react怎么更新state”,内容详细,步骤清晰,细节处理妥当,希望这篇“react怎么更新state”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

react更新state方法有:1、通过key变化子组件,代码如“<Children key={this.state.key} a={this.state.a} b={this.state.b} />”;2、利用ref父组件调用子组件函数;3、通过父级给子级传数据,子级只负责渲染。

react中父级props改变,更新子级state的多种方法

子组件:

class Children extends Component {
  constructor(props) {
     super(props);
     this.state = {
       a: this.props.a,
       b: this.props.b,
       treeData: '',
       targets: '',
     }
    }
  componentDidMount() {
   const { a, b } = this.state
   const data = {a,b}
   fetch('/Url', {
     data
   }).then(res => {
   if (res.code === 0) {
     this.setState({
     treeData: res.a,
     targets: res.b,
  })
  } else {
   message.error(res.errmsg)
  }
  })
  }
 test(item1, item2) {
   const data = { item1, item2 }
   fetch('/Url', {data}).then(res => {
     if (res.code === 0) {
       this.setState({
         treeData: res.a,
         targets: res.b,
       })
     } else {
       message.error(res.errmsg)
     }
   })
 }
}
export default Children

方法一:巧用key

<Children key={this.state.key} a={this.state.a} b={this.state.b} /> //父组件调用子组件

这种方法是通过key变化子组件会重新实例化 (react的key变化会销毁组件在重新实例化组件)

方法二:利用ref父组件调用子组件函数(不符合react设计规范,但可以算一个逃生出口嘻嘻~)

class father extends Component {
    constructer(props) {
      super(props);
      this.state={
       a: '1',
       b: '2',
      }
      this.myRef
      this.test = this.test.bind(this)
    }
   change() {
     const { a,b } = this.state
     console.log(this.myRef.test(a,b)) // 直接调用实例化后的Children组件对象里函数
    }
render() {
 <Children wrappedComponentRef={(inst) => { this.myRef = inst } } ref={(inst) => { this.myRef = inst } } />  
 <button onClick={this.test}>点击</button>
}
}

注:wrappedComponentRef是react-router v4中用来解决高阶组件无法正确获取到ref( 非高阶组件要去掉哦)

方法三:父级给子级传数据,子级只负责渲染(最符合react设计观念)推荐!!

父组件:

class father extends Component {
    constructer(props) {
      super(props);
      this.state={
       a:'1',
       b:'2',
       data:'',
      }
    }
  getcomposedata() {
    const { a, b } = this.state
    const data = { a, b }
    fetch('/Url', {data}).then(res => {
      if (res.code === 0) {
        this.setState({
          data:res.data
        })
      } else {
        message.error(res.errmsg)
      }
    })
  }
render() {
 <Children data={this.state.data}} />  
}
}

子组件:

  componentWillReceiveProps(nextProps) {
    const { data } = this.state
    const newdata = nextProps.data.toString()
    if (data.toString() !== newdata) {
      this.setState({
        data: nextProps.data,
      })
    }
  }

注:react的componentWillReceiveProps周期是存在期用改变的props来判断更新自身state

读到这里,这篇“react怎么更新state”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. React中props与state的区别
  2. React中state和props

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

react state

上一篇:ie无法识别react如何解决

下一篇:Python Django的模型建立与操作方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》