Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  181] [ 3]  / answers: 1 / hits: 40450  / 4 Years ago, tue, march 17, 2020, 12:00:00

I am currently building a card section which shows a horizontal list of cards. This list overflows, which makes the user have to scroll horizontally to view the offscreen cards.



To make this process easier for the user, I want to create buttons which scroll the horizontal list to the left or right.



I tried solving this by creating a reference to the horizontal list and apply a scrollX when I click on the previously mentioned button. This however, results in the following error:



Cannot add property scrollX, object is not extensible


My code:



const ref = useRef(null);

const scroll = () => {
ref.scrollX += 20;
};

return (
<div className={style.wrapper} id={id}>
<Container className={style.container}>
<Row>
<Col>
<button onClick={scroll}>TEST</button>
</Col>
</Row>
</Container>
<div className={style.projects} ref={ref}>
{projects.map((data, index) => (
<CardProject
key={index}
title={data.title}
image={data.image}
description={data.description}
/>
))}
</div>
</div>
);

More From » reactjs

 Answers
29

In order to access a DOM Node with a Ref, you need to use ref.current; useRef is a container for a DOM node and you access that node with the current property.



Additionally, scrollX is a read-only property; you need to call scrollLeft to change the scroll position. For scrollLeft to work, you need to add an overflow-x: scroll; rule to the style.projects for it to work. (If style.projects is an object than change to overflowX: 'scroll'.)



To be able to scroll left or right, your can add a parameter to the function for the scroll offset, so it doesn't always scroll right:



const scroll = (scrollOffset) => {
ref.current.scrollLeft += scrollOffset;
};


For this to work, you'll need two buttons in your JSX that pass in the offset values for left or right to the scroll function:



 <Row>
<Col>
<button onClick={() => scroll(-20)}>LEFT</button>
<button onClick={() => scroll(20)}>RIGHT</button>
</Col>
</Row>

[#51124] Wednesday, March 4, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
micayla

Total Points: 148
Total Questions: 92
Total Answers: 109

Location: Aruba
Member since Sat, Oct 2, 2021
3 Years ago
micayla questions
Fri, Dec 24, 21, 00:00, 2 Years ago
Thu, Apr 16, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
;