React update useState() when variable value changes

shanksu 218

How to update UseState() when variable 'matchUrl' changes. when navigating to another route the 'matchUrl' value changes. and the changes is reflected on console.log and on the 'h1' tag. but not the usestate(), it keeps the old value.

import React, {useState} from 'react'
import useInfinityScroll from './hooks/useInfinityScroll'
import names form './data/names'    

function TableList({ match }) {
   let matchUrl = match.params.name

   const dataMatch = names.filter(name => name.occupation === matchUrl)

   const [listNames, setListNames] = useState(dataMatch.slice(0, 10))

   const [isFetching, setIsFetching] = useInfinityScroll(fetchMoreListItems) //fetches more items when scrolling to bottom of page


   function fetchMoreListItems() {
    setTimeout(() {
      setListNames([...dataMatch.slice(0, 20)])
      setIsFetching(false)
    }, 2000)
   }


   return (
     <>
       <h1>{matchUrl}</h1>

       <div>
         {listNames.filter(name => name.occupation === matchUrl).map(listNames => <Table key={listNames.id} name={listNames} />)}
       </div>
     </>
   )
}

 export default TableList
K.vindi

useState hook takes initial value of the state as an argument, but it only assign the initial value to the state at the component mounting phase, not during re-rendering of the component if anything changes. Therefore, after initial rendering, state won't be changed even if the dataMatch variable changes.

Therefore, you need to have useEffect hook having dependency on dataMatch variable to keep track on its changes. Then you will be able to update the state when you've component re-rendering.

Use the following way to update the state.

  useEffect(() => {
    setListNames(dataMatch.slice(0, 10));
  }, [dataMatch]);

For more information on useEffect hook, please refer https://reactjs.org/docs/hooks-effect.html

Hope this will solve your issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to update DOM when value changes in controller

React-Native update UI when value on element in array changes, without using ForceUpdate

Update observable when input value changes

Update control value when observing changes

setInterval in React component gets duplicated when useState value changes

React useState() doesn't update value synchronously

Why is react text input not changing when useState array element changes?

React useState, value of input doesnt update

React useState update based on a variables previous value

How to render a React functional component when useState() changes?

React state created with useState doesn't update automatically when my API changes

UseState loads previous state when value changes (NextJS)

How to update a dictionary value when a variable changes inside a function

React: useState is showing me the previous state value whereas it changes when I look in the DevTools

useState value changes when i call it from child

React - use variable as value for HTML element and change it's value when variable changes

React onClick update useState variable and render updated

React - how to update the value using useState dynamically

SwiftUI: @State variable does not update View when its value changes (within same View)

React JS update classes when useState is updated

How to update useState() when value changes in React

React useState Dependency Won't Update When Same Value Passed

How to update a useState which has an array, inside this array I have objects this objects will update when the input value will change in react js

React usestate not update value object

OnSubmit - Update view when variable changes

Update variable when react state changes

React does not update a string variable in real-time correctly when using useState. Why is that?

value of variable not changing using useState Hook in React JS when implemented using onClick button

React hook form doesn't see the changes of input when value is useState