• 技术文章 >web前端 >前端问答

    react更新state方法有哪些

    藏色散人藏色散人2022-11-09 15:31:03原创195

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

    大前端成长进阶课程:进入学习

    本教程操作环境:Windows7系统、react17.0.1版、Dell G3电脑。

    react更新state方法有哪些?

    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视频教程

    以上就是react更新state方法有哪些的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:state React
    上一篇:ie无法识别react怎么办 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 总结Vue创建响应式数据对象(reactive、ref、toRef、toRefs)• 详解vue3中reactive和ref的区别(源码解析)• 原理详解:Vue3中reactive和ref的区别• 什么是React高阶组件?怎么它创建一个面包屑导航?• 7 个很棒且实用的React 组件库(压箱底分享)
    1/1

    PHP中文网