score:4
Accepted answer
import React from "react";
import { render } from "react-dom";
class Item extends React.Component {
render() {
const { item } = this.props;
return (
<div style={{ padding: `12px` }} id={item}>
Item {item}
</div>
);
}
}
class List extends React.Component {
state = {
items: [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20
]
};
onClick = () => {
/** You can avoid using getElementById if you can get the div rendered by Item component using refs.
* You can look at refs forwarding and other technics to see how you can solve this */
const divToScrollTo = document.getElementById(`${this.ref_20.props.item}`);
if (divToScrollTo) {
divToScrollTo.scrollIntoView();
}
};
render() {
const { items } = this.state;
return (
<div>
<h1>My List</h1>
<button onClick={this.onClick}>Clickidy</button>
<div
style={{
height: `200px`,
overflow: "scroll",
backgroundColor: "red"
}}
>
{this.state.items.map(item => (
<Item ref={inst => (this[`ref_${item}`] = inst)} item={item} />
))}
</div>
</div>
);
}
}
Source: stackoverflow.com
Related Query
- React scroll component to view inside overflow hidden element
- Scroll View inside view not working react native
- React router v4 render component inside element
- react native Scroll View doesn't scroll from inside text input
- React applying JQuery code to an element inside a component
- React Force horizontal scrolling as vertical scroll when reaching element view
- Add html element inside of react component
- Recursively enter nested object into tree view component inside return statement of a React functional component
- In a large component in React I want to click on one element and scroll to another element
- React change view and then scroll to element
- Mouse Wheel / Scroll event in React element without overflow (picture zoom)
- How to detect scroll up and down inside a div element in react js?
- React component inside an SVG element
- Render a React element inside a React component
- Changing an element inside a component in react
- How to find a React component inside an element with specific class using enzyme?
- Display custom third party html element inside a React component
- React functional component inside a loop calls api only returns the response for last element passed on api
- React parsing function inside component as React Element
- How can I get the ref or info of the React component that is scrolled into view during a vertical scroll using React Scrollbars?
- Difference between React Component and React Element
- Using async/await inside a React functional component
- React - getting a component from a DOM element for debugging
- React Hooks Error: Hooks can only be called inside the body of a function component
- React Warning: Cannot update a component from inside the function body of a different component
- Can I disable a View component in react native?
- Checking text appears inside an element using react testing library
- How do I get an attribute of an element nested in a React component using Jest and/or Enzyme?
- Function inside functional component in React hooks - Performance
- Rendering a React component inside a Bootstrap popover
More Query from same tag
- React component not rendering after state change from callback
- How can I call parent function in a child React component?
- localStorage with React behaving very erratically
- Async Await of imported asynchronous function not waiting correctly
- how to dynamically parse Json data using reactjs
- Jest test Create React APP index.js with new version 18
- Why popstate doesnt fire my function when back button is clicked? (mobile ios chrome)
- Refreshing Heroku Page Only Shows API Information, Not Page Content
- "Error: socket hang up" error displayed when using Postman with ReactJS and MongooseDB
- React Router v6 Route element cannot get Context value
- How to convert the Prices on my ProductCard to another Currency in React JS
- React Typescript: Pass function as props with arguments
- JSX instead of HTML in Sweet Alert
- getting latitude and longitude from react-leaflet when moving the mouse
- Can the vanilla JS date object be used normally in React.js?
- How to display array element bu opening modal?
- Need to show only one month in range picker selection area antd
- How to change text, icon and underline color of Select in Material UI
- How to make a button inside of the anchor tag clickable in React?
- React JS Child objects not showing as valid
- Material-table: how to give custom style to EmptyDataSourceMessage
- How to split dynamically by directories with Webpack/SplitChunks plugin?
- Creating objects from states
- How can I check whether an element is visible with Jest?
- setup initial option value to --select-- while rendering dynamic value in reactjs
- Alternative for http-backend-mock and ngE2e for a react application
- How to set defalulValue or stored value in react (browser extention)
- async load iframe in react
- 'Box' component vs css prop for layout, what to choose?
- Why does this Material-UI Grid item move when I click on it?