I am changing the background color of a table cell when it is fully visible.(当表格单元格完全可见时,我将更改其背景颜色。)
To accomplish this task I have used an intersection observer.(为了完成此任务,我使用了交叉口观察器。)
All the code is available on code sandbox with demo reproducing the bug:(所有代码都可以在代码沙箱中获得,演示重现了该错误:)
I am using useInView hook for the intersection observer:(我在交叉观察器中使用useInView钩子:)
export const useInView = options => {
const ref = useRef();
const [isVisible, setIsVisible] = useState(false);
const [intersectionRatio, setIntersectionRatio] = useState(false);
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
console.log("called");
setIsVisible(entry.isIntersecting);
setIntersectionRatio(entry.intersectionRatio);
}, options);
if (ref.current) observer.observe(ref.current);
return () => {
if (ref.current) observer.unobserve(ref.current);
};
}, [ref, options]);
return [ref, isVisible, intersectionRatio];
};
Here is the table in which I am rendering the data:(这是我在其中渲染数据的表:)
<div id="my-table" style={{ height: 200, width: 200, overflow: "auto" }}>
<table>
<tbody>
{tableValues.map((row, rowIndex) => (
<tr key={rowIndex}>
{row.map((cell, cellIndex) => (
<CellRendererContainer
key={`${rowIndex}${cellIndex}`}
rowIndex={rowIndex}
cellIndex={cellIndex}
tableCell={cell}
/>
))}
</tr>
))}
</tbody>
</table>
</div>
The intersection observer is used in CellRenderer, which is divided into two files:(相交观察器在CellRenderer中使用,分为两个文件:)
CellRendererContainer.js(CellRendererContainer.js)
const CellRendererContainer = ({ rowIndex, cellIndex, tableCell }) => {
const [ref, isVisible, intersectionRatio] = useInView({
root: document.querySelector("#my-table"),
rootMargin: "0px",
threshold: 0.0
});
return (
<CellRenderer
ref={ref}
isVisible={isVisible}
intersectionRatio={intersectionRatio}
rowIndex={rowIndex}
cellIndex={cellIndex}
tableCell={tableCell}
/>
);
};
And here is the actual rendering of the cell, CellRenderer.js(这是单元格的实际渲染CellRenderer.js)
const CellRenderer = React.forwardRef(
({ isVisible, intersectionRatio, rowIndex, cellIndex, tableCell }, ref) => (
<td
ref={ref}
style={{
padding: 25,
backgroundColor:
rowIndex > 0 && cellIndex > 0 && isVisible && intersectionRatio > 0.9
? "red"
: "white"
}}
>
{tableCell}
</td>
)
);
The problem in the current implementation is that: intersection observer's callback for some items is not being called.(当前实现中的问题在于:没有调用交叉观察者对某些项目的回调。)
Expected result:(预期结果:)
Any cell that becomes visible should have a red background as soon as it is visible fully.(完全可见的任何可见单元格应具有红色背景。)
Otherwise, that cell should be white.(否则,该单元格应为白色。)
Code sandbox link: (so that you does not need to scroll to the top)(代码沙箱链接:(因此您无需滚动到顶部))
ask by Vishal translate from so