From this array [21, 42, 54, 1, 87, 90, 56, 27, 89]
I am displaying first 3 numbers by default
next button want to shift one step and show as 42, 54, 1
on previous button click want to show one step behind it like 21, 42, 54
but I am stuck
please sey by code what I am doing wrong, I am trying to increase the starting initial index number from which 3 numbers will be picked
import React from "react";
import { useState } from "react";
const App = () => {
const list = [21, 42, 54, 1, 87, 90, 56, 27, 89];
var start = 0;
const handleClick = () => {
start = start + 1;
};
var n = 3;
var items = list.slice(start, n).map((i) => {
return (
<button style={{ margin: 10 }} type="button" class="btn btn-primary">
{i}
</button>
);
});
return (
<div>
<div onClick={handleClick}>Click</div>
<div>{items}</div>
</div>
);
};
export default App;
3
Answers
First, you should store the
start
index in state.In addition, the second argument to
slice
should be the end index, not the number of elements to take. So the call toArray#slice
should be like this:Working example:
You cannot use variables like that inside a react component because at every render the value of 0 will be set to your
start
variable.Instead you should use
states
. See the working example:Note that your slice code was also wrong since you are using a fixed
n
. Slice parameters are start and end index to slice, so you need to sum start and n.The key is to store the index in state (
const [index, setIndex] = useState(0);
), and to use that state to guide how you’re going to slice up the listlist.slice(index, index + 3)
. Remember that how a component is rendered depends on how it "reacts" to changes in state.A few additional pointers:
If you’re looking to have previous and next buttons you can implement those quite easily by using your
handleClick
function to cover both, and toggle the state depending on which button is clicked.You can disable the buttons when
index
reaches 0, or index reaches the upper bounds of the list length.Make sure that any component returned by a mapping process has a key (as shown below) or React will start yelling at you. Normally this key should correspond with an id in the data you’re iterating over. In this case it’s probably ok to use the
map
index parameter for this purpose (though it generally not recommended. Neither is usinguuid
FWIW).