美文网首页
前端系列(三)react获取子组件属性的方法

前端系列(三)react获取子组件属性的方法

作者: onmeiei | 来源:发表于2021-10-29 11:11 被阅读0次

组件组合有几种情况

  • 父组件不保存任何state,子组件A和子组件B各自保存state
  • 父组件保存state,子组件A和子组件B不保存state,数据由父组件维护
组件布局

父组件不保存任何state,子组件A和子组件B各自保存state

这种情况,如果需要在父组件进行RESTful调用时,需要子组件A和子组件B提供操作数据的方法(主要是初始化和数据获取)

需要使用forwardRef来实现,具体做法如下(以FC为例)

const EcParent: React.FC = () => {
    const childRefA = React.createRef<EcRef<EcChildBean>>()
    const childRefB = React.createRef<EcRef<EcChildBean>>()
    return <>
        <EcChild bean={{ username: "A", age: 1 }} ref={childRefA} />
        <EcChild bean={{ username: "B", age: 2 }} ref={childRefB} />
        <button onClick={() => {
            console.info(JSON.stringify(childRefA.current?.get()))
            console.info(JSON.stringify(childRefB.current?.get()))
        }} >click</button>
    </>
}
export { EcParent }
const EcChild = React.forwardRef<EcRef<EcChildBean>, EcProps<EcChildBean>>((props, ref) => {
    const [username, setUsername] = useState<string>(props.bean?.username || "")
    const [age, setAge] = useState<number>(props.bean?.age || 10)

    useEffect(() => {
        console.log("hello, props, child ===> ", JSON.stringify(props))
    }, [props])

    useImperativeHandle(ref, createEcRef<EcChildBean>(() => {
        return {
            username: username,
            age: age,
        }
    }))

    return <div>
        <label htmlFor={"username"}>UserName: </label>
        <input id="username" value={username} type="text" onChange={evt => setUsername(evt.target.value)} />
        <label htmlFor={"age"}>Age: </label>
        <input id="age" value={age} type="number" onChange={evt => setAge(parseInt(evt.target.value))} />
    </div>
})

export default EcChild
export type { EcChildBean }

使用场景,A和B不需要进行通讯,但是与服务器进行RESTful通讯时,需要同时获取A和B的数据。
这样做可以避免操作A的数据时,刷新B的数据。

父组件保存state,子组件A和子组件B不保存state,数据由父组件维护

这种情况,如果需要在父组件进行RESTful调用时,可以直接使用父组件维护的数据进行处理。

缺点是,每次子组件操作数据,由于操作的都是父组件的state,会导致父组件的所有子组件进行刷新。如果组件数量特别多,性能较上一个方案差一些

const EcParent: React.FC = () => {

    const [childA, setChildA] = useState<EcChildBean>({ username: "A", age: 1 })
    const [childB, setChildB] = useState<EcChildBean>({ username: "B", age: 2 })

    return <>
        <EcChild bean={childA} onChange={a => setChildA(a)} />
        <EcChild bean={childB} onChange={b => setChildB(b)} />
        <button onClick={() => {
            console.info(JSON.stringify(childA))
            console.info(JSON.stringify(childB))
        }} >click</button>
    </>
}
export { EcParent }
interface EcChildBean {
    username: string
    age: number
}

const EcChild: React.FC<EcProps<EcChildBean>> = (props) => {
    useEffect(() => {
        console.log("hello, props, child ===> ", JSON.stringify(props))
    }, [props])


    return <div>
        <label htmlFor={"username"}>UserName: </label>
        <input id="username" value={props.bean?.username} type="text" onChange={evt => props.onChange({ ...props.bean, username: evt.target.value })} />
        <label htmlFor={"age"}>Age: </label>
        <input id="age" value={props.bean?.age} type="number" onChange={evt => props.onChange({ ...props.bean, age: parseInt(evt.target.value) })} />
    </div>
}

export default EcChild
export type { EcChildBean }

源码已经上传到了Ocean/reactjs-comm-demo (gitee.com)

可以通过console中打印的useEffect方法的日志,看出两种方案的区别。

相关文章

  • 前端系列(三)react获取子组件属性的方法

    组件组合有几种情况 父组件不保存任何state,子组件A和子组件B各自保存state 父组件保存state,子组件...

  • React获取DOM

    React获取DOM的方法简述 createRef 当 ref 属性在组件时,获取组件实例;当ref属性在dom时...

  • react中父组件调用子组件的方法

    react中父组件调用子组件的方法 最近项目中用到了react,需要在父组件中调用子组件的某个方法,那么如何获取到...

  • 用vue-cli开发项目时候的一些工作笔记

    1、父组件: 获取子组件的方法属性:this.refs.headerChild.方法子组件获取父组件的数据: 2...

  • 父组件获取子组件的数据和方法

    #调用子组件的时候定义一个 ref #如何使用子组件数据和方法 #子组件主动获取父组件里的属性和方法

  • React入门(二)

    三、React组件 React 组件基本上是由组件的构建方式、组件内的状态属性与生命周期方法组成 React.cr...

  • Vue父子组件间传值

    一、父组件向子组件传值(通过 props 属性) 父组件 子组件 子组件方法中通过 this.value 来获取数...

  • React组件间通信

    父组件向子组件 定义组件的方法就是通过React.createClass,当我们使用组件并且给其属性的时候,这个属...

  • react子组件向父组件传值

    相关资料:react 父组件怎么获取子组件的这个值React组件间信息传递方式react同级组件之间传值 • 父...

  • vue3组件之间传值

    1.父子组件之间的传值:props 2.子组件向父组件传值emit 3.父组件获取子组件的属性或者调用子组件方法r...

网友评论

      本文标题:前端系列(三)react获取子组件属性的方法

      本文链接:https://www.haomeiwen.com/subject/hrkialtx.html