Reactjs 为什么通过useState钩子声明的状态不变?
方法switchSection应更改当前显示部分的索引以显示下一个部分,索引被声明为当前状态,每当用户单击enter键switchSection方法执行时,如果当前显示的部分是列表中最新的部分,则setCurrent应设置为零,否则增加1。一切似乎都很好,但setCurrent不会修改当前状态Reactjs 为什么通过useState钩子声明的状态不变?,reactjs,react-hooks,setstate,use-state,Reactjs,React Hooks,Setstate,Use State,方法switchSection应更改当前显示部分的索引以显示下一个部分,索引被声明为当前状态,每当用户单击enter键switchSection方法执行时,如果当前显示的部分是列表中最新的部分,则setCurrent应设置为零,否则增加1。一切似乎都很好,但setCurrent不会修改当前状态 export default () =>{ const refs = [] //Here is the state declaration const [current, setCu
export default () =>{
const refs = []
//Here is the state declaration
const [current, setCurrent] = useState(0)
useEffect(() => {
window.addEventListener('keyup', switchSection)
return _ => window.removeEventListener('keyup', switchSection)
}, [])
const switchSection = event => {
if(event.keyCode === 13) {
refs[current].current.classList.remove('activeTabLink')
//the line below does not change the state, the current remains 0
current < refs.length - 1 ? setCurrent(prevVal => prevVal + 1) : setCurrent(0)
refs[current].current.classList.add('activeTabLink')
}
}
const changeActiveElement = (event) => {
let buttons = Array.from(document.getElementsByClassName('tabLinks'))
buttons.forEach(button => button.classList.remove('activeTabLink'))
event.target.classList.add('activeTabLink')
setCurrent(parseInt(event.target.id))
}
return (
<div style={SSS()}>
<Chapter additionalStyle={{textAlign: 'left', marginLeft: '2%'}}>Chapter</Chapter>
<div className="tab">
{history.links.map((link, index) => {
const newRef = useRef(null);
refs.push(newRef);
if (index === 0) return <button className="tabLinks activeTabLink" ref={newRef} id={index} onClick={changeActiveElement} key={index}>{link}</button>
else return <button className="tabLinks" ref={newRef} id={index} onClick={changeActiveElement} key={index}>{link}</button>
})}
</div>
<div className="aboutContent">
</div>
</div>
)
}
导出默认值()=>{
常数参考值=[]
//这是国家声明
常数[current,setCurrent]=useState(0)
useffect(()=>{
window.addEventListener('keyup',switchSection)
return=>window.removeEventListener('keyup',switchSection)
}, [])
const switchSection=事件=>{
如果(event.keyCode===13){
refs[current].current.classList.remove('activeTabLink')
//下一行不改变状态,电流保持为0
电流prevVal+1):设置电流(0)
参考文献[current].current.classList.add('activeTabLink')
}
}
常量changeActiveElement=(事件)=>{
let buttons=Array.from(document.getElementsByClassName('tabLinks'))
buttons.forEach(button=>button.classList.remove('activeTabLink'))
event.target.classList.add('activeTabLink')
setCurrent(parseInt(event.target.id))
}
返回(
章
{history.links.map((链接,索引)=>{
const newRef=useRef(null);
参考推送(新参考);
if(index==0)返回{link}
else返回{link}
})}
)
}
大宗报价
我假设您已经检查了
current
是否实际计算为true
?那将是我第一个去看的地方,但我没有足够的信息来检查。检查这一点的一个好方法是删除三元运算符,使用if-else并插入console.log或在调试器中运行并逐步执行
接下来,请记住以下几行:
current < refs.length - 1 ? setCurrent(prevVal => prevVal + 1) : setCurrent(0)
尽管如此,我还是建议您这样做,这样您就不会关闭上一次渲染的值:
setCurrent(prevVal => {
return prevVal < refs.length - 1 ? prevVal + 1 : 0;
});
setCurrent(prevVal=>{
返回prevVal<参考长度-1?prevVal+1:0;
});
但这可能不是你的错误。我猜你的
setCurrent(prevVal=>prevVal+1)
永远不会被调用。我假设你已经检查过current
的计算结果实际上是true
?那将是我第一个去看的地方,但我没有足够的信息来检查。检查这一点的一个好方法是删除三元运算符,使用if-else并插入console.log或在调试器中运行并逐步执行
接下来,请记住以下几行:
current < refs.length - 1 ? setCurrent(prevVal => prevVal + 1) : setCurrent(0)
尽管如此,我还是建议您这样做,这样您就不会关闭上一次渲染的值:
setCurrent(prevVal => {
return prevVal < refs.length - 1 ? prevVal + 1 : 0;
});
setCurrent(prevVal=>{
返回prevVal<参考长度-1?prevVal+1:0;
});
但这可能不是你的错误。我猜你的
setCurrent(prevVal=>prevVal+1)
永远不会被调用。你的错误就在这里。setCurrent(prevVal=>prevVal+1)将该逻辑抽象为一个常数,并在三元上再次传递它。错误在这里。setCurrent(prevVal=>prevVal+1)将该逻辑抽象为一个常数,并将其再次传递给三元函数。是的,setCurrent(prevVal=>{return prevVal