M Muzzammil ijaz
M Muzzammil ijaz

Reputation: 178

is it good to use following in react hooks or any alternative to update the UI

here is my code i am facing the issue that the UI not reRendering after the state changes

const [, setRand] = useState()
setRand(Math.random())

Upvotes: 0

Views: 72

Answers (2)

Dev K
Dev K

Reputation: 35

You better use key attribute for re-rendering

return (
  <SomeOtherComponent>
    <Component key={dependencyVariable} />
  </SomeOtherComponent>
)

Upvotes: 0

Ertan Hasani
Ertan Hasani

Reputation: 1773

you cannot use useState as const [, setRand] = useState();

Here is the description from ReactJs documentation.

You need to use it like this

const [random, setRandom] = useState(0);
//or you can just set random in the beginning like
const [random, setRandom] = useState(Math.random());

And make sure you are accessing it somewhere on return() method like:

<p>{ random }</p>

Upvotes: 1

Related Questions