Я меняю цвет фона ячейки таблицы, когда она полностью видна. Для выполнения этой задачи я использовал наблюдатель перекрестков.
Весь код доступен в песочнице кода с демонстрацией, воспроизводящей ошибку:
Я использую хук 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];
};
Вот таблица, в которой я визуализирую данные:
<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>
Наблюдатель пересечения используется в CellRenderer, который разделен на два файла:
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}
/>
);
};
А вот фактический рендеринг ячейки 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>
)
);
Проблема в текущей реализации заключается в том, что: обратный вызов наблюдателя пересечения для некоторых элементов не вызывается.
Ожидаемый результат:
Любая ячейка, которая становится видимой, должна иметь красный фон, как только она становится видимой полностью. В противном случае эта ячейка должна быть белой.
Ссылка на песочницу кода: (чтобы не нужно было прокручивать вверх)
console.log
может помочь в отладке.) - person Ouroborus   schedule 26.11.2019