简体   繁体   English

应该每次返回不同值的 function 在反应中不起作用

[英]a function that supposed to return a different value each time does not work in react

I am working on my portfolio, but when I reach the experiences page I tried to create a title that has it's middle part change every second, and value of it must come from an already set up array, but when I run the code it always return the first string of the array, can anyone please fix this problem for me?我正在研究我的作品集,但是当我到达体验页面时,我尝试创建一个标题,它的中间部分每秒都在变化,它的值必须来自已经设置的数组,但是当我运行代码时它总是返回数组的第一个字符串,谁能帮我解决这个问题?

const projectsTitleKeyWords = ['responsible', 'meaningful', 'beautiful']
let titlep2 = 'test'
let index = 0

const change = () => { 
  titlep2 = projectsTitleKeyWords[index]
  index = ++index % projectsTitleKeyWords.length
  setTimeout(change, 1000)
}
change()
console.log(titlep2)


const titlep1 = 'I creat '
const titlep1Array = titlep1.split('')
let titlep2Array = titlep2.split('')
const titlep3 = ' projects'
const titlep3Array = titlep3.split('')

the value of titlep2Array will be received by titlep2Array 的值将由

<AnimatedLetters
   letterClass={letterClass}
   strArray={titlep2Array}
   idx={15}
   id='to-change'
/>

In-order to reflect UI changes in React, a component must re-render.为了反映 React 中的 UI 变化,组件必须重新渲染。

A React component re-renders in 1 of 2 scenarios: React 组件在 2 个场景中的 1 个中重新渲染:

  1. Whenever there's a change in the value of a local state.每当本地 state 的值发生变化时。
  2. Whenever any of it's parent components re-render.每当它的任何父组件重新渲染时。

Therefor, since changes in the UI are only reflected upon a re-render, we should manage a local state that would be responsible for this behavior.因此,由于 UI 中的更改仅反映在重新渲染时,我们应该管理一个负责此行为的本地 state。
With functional components in React, this can be achieved via the useState hook.使用 React 中的功能组件,这可以通过useState挂钩来实现。

In your case, we can simply make titlep2 a state, instead of a regular variable.在您的情况下,我们可以简单地将titlep2设为 state,而不是常规变量。

Example:例子:

const [titlep2, setTitlep2] = useState('')

const change = () => { 
  setTitlep2(projectsTitleKeyWords[index])
  index = ++index % projectsTitleKeyWords.length
  setTimeout(change, 1000)
}

<AnimatedLetters
   letterClass={letterClass}
   strArray={titlep2.split('')}
   idx={15}
   id='to-change'
/>

Note: since this function now updates the state, we can't call it the way you did in your example, since it will run every time the component re-renders, making the component re-render indefinitely due to the change in state.注意:由于此 function 现在更新了 state,因此我们不能像您在示例中那样调用它,因为它会在每次组件重新渲染时运行,由于 Z6EF739E2A69352 中的更改,组件会无限期地重新渲染

Therefor, we can use the useEffect hook in-order to allow it to run only once on the initial render.因此,我们可以使用useEffect钩子以允许它在初始渲染时只运行一次。

Example:例子:

const change = () => { 
  setTitlep2(projectsTitleKeyWords[index])
  index = ++index % projectsTitleKeyWords.length
  setTimeout(change, 1000)
}

useEffect(() => {
   change()
}, [])
  • Furthermore, if there are any other variables that should reflect changes in the UI, they can be convert to states as well.此外,如果有任何其他变量应该反映 UI 中的变化,它们也可以转换为状态。

for that try using the setInterval() instead of setTimeout() .为此尝试使用setInterval()而不是setTimeout()

You are trying to make the text change after a specific interval in this case 1 second.在这种情况下,您尝试在特定间隔 1 秒后更改文本。

You should also consider doing that will CSS animations, it seems this is overkill.您还应该考虑这样做 CSS 动画,这似乎是矫枉过正。

const change = () => { 
  titlep2 = projectsTitleKeyWords[index]
  index = ++index % projectsTitleKeyWords.length
  
  console.log(titlep2)
  setInterval(change, 1000)
}
change()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM