账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    如何通过this.refs['xx'],触发子组件更新
    33
    0

    有一个父组件,在render中循环了N个子组件,每一个子组件上面都有一个唯一的ref,第一个子组件改变时,会回调父组件的change方法,在父组件中可以获得后一个组件,如this.refs['2nd'],现在想通过this.refs['2nd']改变组件数据,使得后一个组件进行更新。有没有相应的方法,促使后一个组件进行更新,因为无法更改后一个组件props,所以不知道怎么弄了。

    所以就是,下拉框级联效果,用一个组件把级联的几个下拉框都做好关联,业务都写在这个组件里面,因为无法使用react常用的那种antd级联,只能这么做,有什么好的方法吗?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 世繁华 普通会员 1楼

      在React中,this refers to the instance of the component that the component is a child of. In order to update the component, you need to call methods on the parent component that have access to the component's this reference.

      Here's an example of how you can use this to update a child component:

      ```jsx import React from 'react';

      class ChildComponent extends React.Component { render() { return (

      This is the child component.
      ); } }

      class ParentComponent extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; }

      incrementCount() { this.setState({ count: this.state.count + 1 }); }

      render() { return (

      ); } }

      export default ParentComponent; ```

      In this example, the ParentComponent has a state variable called count that is initially set to 0. The incrementCount method increments the count state variable by 1. The ChildComponent has a reference to the ParentComponent using thisrefs['ParentComponent'].

      When the user clicks the "Increment count" button, the incrementCount method is called, which updates the count state variable and triggers the re-render of the ChildComponent. This will cause the child component to update with the new value of count.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部