英文:
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组件 A
和 B
,每个组件中都包含一个 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
都需要一个指向自身和对齐组件 alignedGrids
的 ref
,类似于:
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 'react';
const A = props => {
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
import React, {useRef} from 'react';
const B = props => {
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
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?
答案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 '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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论