How to update state by useEffect in React?

Viewed 275

I'm newbie in React. I try to make a project with price calculator which is prepared in 2 languages. Everything is based on Factory Pattern. To change language I used i18next framework.Static text elements on website change dynamically and everything works fine.Problem is when I try change to another language object logo.text stay the same. I know that useState render once so how can i use useEffect? or is another solution to solve render problem ? I put few elements to calculate in array of objects in useState. Looks something like this:

  const [logo, setLogo] = useState([
{
  type: "tocheck",
  checked: false,
  name: "rights",
  text: t("rightsLogo.text"),
  price: 200,
},])
3 Answers

You'll need to have a language state or prop and you need to call useEffect with language in the dependency array and update the logo state using setLogo inside the useEffect.

useEffect(() => {
    setLogo({
        //your logo object here
    })
}, [language])

You can just try out use effect to render it by putting logo as dependency variable something like this:

useEffect = ({},[logo]) 

What above basically does is whenever the state of logo changes it re renders it.

You would also need to actually use setState for the logo state to change

Try this , it might solve your problem

 useEffect(() => {
   // thing you want to update
}, [JSON.stringi(logo)])
Related