score:-1
You can Use CSS option to set the scroll behavior across the app smooth.
html {
scroll-behavior: smooth;
}
score:0
If you are using ref
then assign it to the target element:
const top= useRef();
assign to this here:
return(
<div ref={top}/>
{list()}
);
score:4
I'd prefer @Partha Roy solution myself, but if you like more control, use window.scroll()
Try this:
const App = () => {
const elementRef = React.useRef(null)
const toTop = () => {
window.scroll({
top: 0,
left: 0,
behavior: "smooth"
})
}
// If back to element (use Ref)
const toElemnt = () => {
if(!elementRef) return
// Get element coords from Ref
const element =
elementRef.current.getBoundingClientRect().top + window.scrollY
window.scroll({
top: element,
behavior: "smooth"
})
}
return (
<div className="App">
<div className="content" style={{ height: 1500 }}>Scroll Down <span>↓</span>
<div ref={elementRef} style={{ marginTop: 750 }}>
Element - continue scrolling <span>↓</span>
</div>
</div>
<button onClick={toElemnt}>Back to Element <span>↑</span></button>
<button onClick={toTop}>Back to to <span>↑</span></button>
</div>
)
}
ReactDOM.render(<App />, document.getElementById("root"))
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
Source: stackoverflow.com
Related Query
- ScrollIntoView behavior smooth is broken in react
- React router Switch behavior
- Browser navigation broken by use of React Error Boundaries
- How to check for broken images in React JS
- How to Add Smooth Scrolling Back To Top Button using react js?
- React Navigation weird back button behavior in nested navigators
- React - weird behavior when useState hook sets state for the first time vs subsequent times
- Strange behavior of React hooks: delayed data update
- scrollIntoView using React refs
- React smooth transition between different states in a component
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- React .createClass() scroll to ref: scrollIntoView not a function
- React Native - Jest: Broken since update to 0.56. How to fix it?
- Hide broken image link in Semantic UI React
- How to get consistent ScrollIntoView behavior
- React Hooks - Passing ref to child to use ScrollIntoView
- How do I enable smooth animation during dynamic addition of components in react native?
- Weird behavior on event handlers in React
- testing click behavior on React Router Links with Enzyme
- Strange behavior when parsing JSON cookie using cookie-react in React / NextJS
- Strange React Compiler Behavior
- React setInterval Behavior
- React Events behavior
- Change behavior of Steps React component from Ant Design(https://ant.design/components/steps/)
- React Material-UI menu anchor broken by react-window list
- Semantic React Modal weird height behavior
- Emulating DOM onchange behavior in React
- Testing for text broken up by multiple elements in React Testing Library
- Smooth out parallax in React component
- React - Navigation that will smooth scroll to section of the page
More Query from same tag
- Make buttons slide up in accordion in React
- Set scroll mode conditionally with react-full-page
- Stop statically served React app from calling server path
- Simulate vs props - On Change event using Jest and Enzyme
- Directly modifying the state of a react component works reliably and consistently?
- Render components vertically rather than horizontally
- cannot find module '@babel/helper-regex' require stack
- initialValues works the first time, but after page refresh breaks the page, REACT/REDUX
- Ajax post call returns parsererror
- Uncaught Error: Cannot find module 'file:/home/agemiro/backend-gerenciador/uploads/imagemteste.png'
- How to specify a list of custom tokens to list for autocompletion in Ace Editor?
- useEffect updates it's own dependency causing infinite loop
- react-particles-js: Can't set a background and width
- HOC for JSX element - rendering jsx with wrapped element
- React: How can I call these functions in useEffect conditionally inside my component?
- React functional component destructuring
- How to make a functional React component with generic type?
- React Context value not defined inside click handler
- 'ReferenceError: Headers is not defined' when using Headers in a server side rendered react project
- How can I render a list by clicking a button in React?
- How store binary tree nodes in firestore
- GeoJSON from react-leaflet doesn't render the layer over the map
- React Router Child Rendering Issue
- How to change the menu item's color of sidebar when we click on it with css in React pls
- ComponentDidUpdate causing infinite render even when wrapped in condition
- using html attributes in JSX
- How to query textarea elements by value using react-testing-library and Jest?
- Can you import an external CSS file in a React js file?
- Add sass/css modules to typescript react app
- What does a null mean in React createElement()?