How can I scroll a div to be visible in ReactJS?

前端 未结 9 1545
梦如初夏
梦如初夏 2020-12-04 15:25

I have a popup list which is a div that contains a vertical list of child divs. I have added up/down keyboard navigation to change which child is

9条回答
  •  盖世英雄少女心
    2020-12-04 16:08

    With reacts Hooks:

    1. Import
    import ReactDOM from 'react-dom';
    import React, {useRef} from 'react';
    
    1. Make new hook:
    const divRef = useRef(null);
    
    1. Add new Div
    1. Scroll function:
    const scrollToDivRef  = () => {
        let node = ReactDOM.findDOMNode(divRef.current) as Element;
        node.scrollIntoView({block: 'start', behavior: 'smooth'});
    }
    

提交回复
热议问题