如何在具有共同父组件的兄弟组件之间传递注册状态?

huangapple go评论156阅读模式
英文:

How to cross register state across sibling components with a shared parent component?

问题

I have two React components A and B which contain each an AgGridReact, actually is one A component and B1, B2, B3 etc. components but ok we can assume is only A and B as only one B will be active at the time.

我有两个React组件 AB,每个组件中都包含一个 AgGridReact,实际上是一个A组件和B1、B2、B3等组件,但可以假设只有一个A和一个B处于活动状态。

I would like to replicate the simple example here for aligning two AgGridReact:
https://www.ag-grid.com/react-data-grid/aligned-grids/#example-aligned-column-groups

我想要复制这里的简单示例,以对齐两个 AgGridReact
https://www.ag-grid.com/react-data-grid/aligned-grids/#example-aligned-column-groups

the issue is that each AgGridReact needs a ref to itself and to the aligned component alignedGrids something like:

问题在于每个 AgGridReact 都需要一个指向自身和对齐组件 alignedGridsref,类似于:

Component A

组件 A

import React, { useRef } from 'react';

const A = props => {
  const aGridRef = useRef(null);
   
  return (
    <div className="container">
      <div className="grid ag-theme-alpine">
        <AgGridReact
           ref={aGridRef}
           alignedGrids={props.bGridRef.current ? [props.bGridRef.current] : undefined}
        />
      </div>        
    </div>
   )
};

export default A;

Component B

组件 B

import React, { useRef } from 'react';

const B = props => {
  const bGridRef = useRef(null);

  return (
    <div className="container">
      <div className="grid ag-theme-alpine">
        <AgGridReact
           ref={bGridRef}
           alignedGrids={props.aGridRef.current ? [props.aGridRef.current] : undefined}
        />
      </div>        
    </div>
   )
};

export default B;

Now assume I have the parent Component X

现在假设我有父组件 Component X

import React, { useRef } from 'react';

const X = props => {
   return (
      <div>
        <A />
        <B />
      </div>
   )
};

export default X;

What should X do to correctly cross register the references in all components?

X 应该怎么做才能正确地在所有组件中跨注册引用?

英文:

I have two React components A and B which contain each an AgGridReact, actually is one A component and B1, B2, B3 etc. components but ok we can assume is only A and B as only one B will be active at the time.

I would like to replicate the simple example here for aligning two AgGridReact:
https://www.ag-grid.com/react-data-grid/aligned-grids/#example-aligned-column-groups

the issue is that each AgGridReact needs a ref to itself and to the aligned component alignedGrids something like:

Component A

import React, {useRef} from &#39;react&#39;;

const A = props =&gt; {
  aGridRef = useRef(null);
   
  return (
    &lt;div className=&quot;container&quot;&gt;
      &lt;div className=&quot;grid ag-theme-alpine&quot;&gt;
        &lt;AgGridReact
           ref={aGridRef}
           alignedGrids={props.bGridRef.current ? [props.bGridRef.current] : undefined}
        /&gt;
      &lt;/div&gt;        
    &lt;/div&gt;
   )
};

export default A;

Component B

import React, {useRef} from &#39;react&#39;;

const B = props =&gt; {
  bGridRef = useRef(null);

  return (
    &lt;div className=&quot;container&quot;&gt;
      &lt;div className=&quot;grid ag-theme-alpine&quot;&gt;
        &lt;AgGridReact
           ref={bGridRef}
           alignedGrids={props.aGridRef.current ? [props.aGridRef.current] : undefined}
        /&gt;
      &lt;/div&gt;        
    &lt;/div&gt;
   )
};

export default B;

Now assume I have the parent Component X

import React, {useRef} from &#39;react&#39;;

const X = props =&gt; {
   return (
      &lt;div&gt;
        &lt;A /&gt;
        &lt;B /&gt;
      &lt;/div&gt;
   )
};

export default X;

What should X do to correctly cross register the references in all components?

答案1

得分: 1

import React, { useRef } from 'react';

const X = props => {
   const aTable = useRef(null)
   const bTable = useRef(null)
   return (
      <div>
        <A ref={aTable} alignWith={bTable} />
        <B ref={bTable} alignWith={aTable} />
      </div>
   )
};

export default X;
import React, { forwardRef } from 'react';

const A = forwardRef((props, ref) => {
  return (
    <div className="container">
      <div className="grid ag-theme-alpine">
        <AgGridReact
           ref={ref}
           alignedGrids={props.alignWith.current ? [props.alignWith.current] : undefined}
        />
      </div>        
    </div>
   )
});

export default A;
import React, { forwardRef } from 'react';

const B = forwardRef((props, ref) => {
  return (
    <div className="container">
      <div className="grid ag-theme-alpine">
        <AgGridReact
           ref={ref}
           alignedGrids={props.alignWith.current ? [props.alignWith.current] : undefined}
        />
      </div>        
    </div>
   )
});

export default B;
英文:
import React, {useRef} from &#39;react&#39;;

const X = props =&gt; {
   const aTable = useRef(null)
   const bTable= useRef(null)
   return (
      &lt;div&gt;
        &lt;A ref={aTable} alignWith={bTable} /&gt;
        &lt;B ref={bTable} alignWith={aTable} /&gt;
      &lt;/div&gt;
   )
};

export default X;
import React, { forwardRef } from &#39;react&#39;;

const A = forwardRef((props, ref) =&gt; {
  return (
    &lt;div className=&quot;container&quot;&gt;
      &lt;div className=&quot;grid ag-theme-alpine&quot;&gt;
        &lt;AgGridReact
           ref={ref}
           alignedGrids={props.alignWith.current ? [props.alignWith.current] : undefined}
        /&gt;
      &lt;/div&gt;        
    &lt;/div&gt;
   )
});

export default A;
import React, { forwardRef } from &#39;react&#39;;

const B = forwardRef((props, ref) =&gt; {
  return (
    &lt;div className=&quot;container&quot;&gt;
      &lt;div className=&quot;grid ag-theme-alpine&quot;&gt;
        &lt;AgGridReact
           ref={ref}
           alignedGrids={props.alignWith.current ? [props.alignWith.current] : undefined}
        /&gt;
      &lt;/div&gt;        
    &lt;/div&gt;
   )
});

export default B;

huangapple
  • 本文由 发表于 2023年6月2日 04:43:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定