admin管理员组文章数量:1023251
I'm having an issue when trying to increment through the index of an array in React. There are three items in the array, which is called 'projects', and I'm using a separate variable to set state of the index.
const [projects, setProjects] = useState([
{
title: 'hello'
},
{
title: 'hi'
},
{
title: 'bye'
},
])
const [index, setIndex] = useState({
i: 0
})
I have a JSX button element, which has an onClick event handler function. The purpose of this function is to increment through the index of the array and display its data on screen.
function handleClick(e) {
if (index.i > 2){
index.i = 2
} else {
setIndex({i: index.i + 1 })
}
e.preventDefault()
}
This function works fine, but only until index.i reaches 2. If it goes above 2 the page breaks because there are only 3 items in the array. Is there any reason why my if statement in the function isn't working? I've tried variations of it, using index.i === 0 instead of >2, but this still doesn't work.
JSX -
<div>{projects[index.i].title}</div>
<button onClick={handleClick}></button>
Please can someone help?! Thanks
I'm having an issue when trying to increment through the index of an array in React. There are three items in the array, which is called 'projects', and I'm using a separate variable to set state of the index.
const [projects, setProjects] = useState([
{
title: 'hello'
},
{
title: 'hi'
},
{
title: 'bye'
},
])
const [index, setIndex] = useState({
i: 0
})
I have a JSX button element, which has an onClick event handler function. The purpose of this function is to increment through the index of the array and display its data on screen.
function handleClick(e) {
if (index.i > 2){
index.i = 2
} else {
setIndex({i: index.i + 1 })
}
e.preventDefault()
}
This function works fine, but only until index.i reaches 2. If it goes above 2 the page breaks because there are only 3 items in the array. Is there any reason why my if statement in the function isn't working? I've tried variations of it, using index.i === 0 instead of >2, but this still doesn't work.
JSX -
<div>{projects[index.i].title}</div>
<button onClick={handleClick}></button>
Please can someone help?! Thanks
Share Improve this question asked Jan 31, 2020 at 15:48 AndreAndre 652 silver badges5 bronze badges 1- Don't mutate state directly; use the hook setters in order to do it. – Petrogad Commented Jan 31, 2020 at 15:55
3 Answers
Reset to default 3Use the length of the projects
First off index should be an int not an object.
const [index, setIndex] = useState(0);
No need for bad syntax like index.i
thats a code smell.
Instead of a static number try project length - 1 since index is counting from 0 and length counts each item.
if (index < projects.length - 1){setIndex(index + 1 )}
I believe it's because the index is already greater than 2 when your if statement is true. Your array index is [0, 1, 2] so an index of 3 breaks it.
Try this code instead:
function handleClick(e) {
e.preventDefault()
if (index.i === (projects.length - 1)) return null
setIndex({i: index.i + 1 })
}
Is there a reason why your index is an object ? It's harder to use, and I don't see the point here.
If not, your state should look like this :
const [projects, setProjects] = useState([
{title: 'hello'},
{title: 'hi'},
{title: 'bye'},
])
const [index, setIndex] = useState(0)
And you will be able to work with it like this :
function handleClick(e) {
e.preventDefault()
setIndex((prevIndex) => {
if (prevIndex >= 2)
return 2
else
return prevIndex + 1
})
}
I'm having an issue when trying to increment through the index of an array in React. There are three items in the array, which is called 'projects', and I'm using a separate variable to set state of the index.
const [projects, setProjects] = useState([
{
title: 'hello'
},
{
title: 'hi'
},
{
title: 'bye'
},
])
const [index, setIndex] = useState({
i: 0
})
I have a JSX button element, which has an onClick event handler function. The purpose of this function is to increment through the index of the array and display its data on screen.
function handleClick(e) {
if (index.i > 2){
index.i = 2
} else {
setIndex({i: index.i + 1 })
}
e.preventDefault()
}
This function works fine, but only until index.i reaches 2. If it goes above 2 the page breaks because there are only 3 items in the array. Is there any reason why my if statement in the function isn't working? I've tried variations of it, using index.i === 0 instead of >2, but this still doesn't work.
JSX -
<div>{projects[index.i].title}</div>
<button onClick={handleClick}></button>
Please can someone help?! Thanks
I'm having an issue when trying to increment through the index of an array in React. There are three items in the array, which is called 'projects', and I'm using a separate variable to set state of the index.
const [projects, setProjects] = useState([
{
title: 'hello'
},
{
title: 'hi'
},
{
title: 'bye'
},
])
const [index, setIndex] = useState({
i: 0
})
I have a JSX button element, which has an onClick event handler function. The purpose of this function is to increment through the index of the array and display its data on screen.
function handleClick(e) {
if (index.i > 2){
index.i = 2
} else {
setIndex({i: index.i + 1 })
}
e.preventDefault()
}
This function works fine, but only until index.i reaches 2. If it goes above 2 the page breaks because there are only 3 items in the array. Is there any reason why my if statement in the function isn't working? I've tried variations of it, using index.i === 0 instead of >2, but this still doesn't work.
JSX -
<div>{projects[index.i].title}</div>
<button onClick={handleClick}></button>
Please can someone help?! Thanks
Share Improve this question asked Jan 31, 2020 at 15:48 AndreAndre 652 silver badges5 bronze badges 1- Don't mutate state directly; use the hook setters in order to do it. – Petrogad Commented Jan 31, 2020 at 15:55
3 Answers
Reset to default 3Use the length of the projects
First off index should be an int not an object.
const [index, setIndex] = useState(0);
No need for bad syntax like index.i
thats a code smell.
Instead of a static number try project length - 1 since index is counting from 0 and length counts each item.
if (index < projects.length - 1){setIndex(index + 1 )}
I believe it's because the index is already greater than 2 when your if statement is true. Your array index is [0, 1, 2] so an index of 3 breaks it.
Try this code instead:
function handleClick(e) {
e.preventDefault()
if (index.i === (projects.length - 1)) return null
setIndex({i: index.i + 1 })
}
Is there a reason why your index is an object ? It's harder to use, and I don't see the point here.
If not, your state should look like this :
const [projects, setProjects] = useState([
{title: 'hello'},
{title: 'hi'},
{title: 'bye'},
])
const [index, setIndex] = useState(0)
And you will be able to work with it like this :
function handleClick(e) {
e.preventDefault()
setIndex((prevIndex) => {
if (prevIndex >= 2)
return 2
else
return prevIndex + 1
})
}
本文标签: javascriptIncrementing through an array index onClick In ReactStack Overflow
版权声明:本文标题:javascript - Incrementing through an array index onClick In React - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745532552a2154815.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论