0

I Create Guttenberg pluggin by react. My problem is that I cannot use if statement.

 const priceforextrashop =  parseFloat(props.priceextra)
 const priceforegoodshop = parseFloat(props.pricegoood)
 const priceforstandardshop = parseFloat(props.pricestandard)
 const priceforsmallshop = parseFloat(props.pricesmall)
   React.useEffect(() => {
     if(wynik<20000){
      setPrice(priceforextrashop);
    }
     if(wynik<15000){
      setPrice(priceforegoodshop);
    }
     if(wynik<10000){
      setPrice(priceforstandardshop);
    }
   if(wynik<5000){
    setPrice(priceforsmallshop);
  }

}, [wynik]);

props.prices reffer to attritubutes I add on editor-block side of plugin. The value work good, I exposed them on screen. problem is just with these if statement because always show the same value. I know that the if statement should work good because on editor-black side or in another scripts, I put the same if statement and it worked. Please indicate me answer.

update:

I add dependence "wynik" but it not resolve my problem. Just count 2from 4 variables not all of them. I try without useEffect but then it make an error.

3
  • Are you trying to use this for controls in the editor, for configuring the block, or are you creating a block that has React interactivity on the front end? Can you include a full block of code so we can see the context of these snippets? Commented Mar 3, 2022 at 8:41
  • Ok, I hope, I update the question correctly Commented Mar 3, 2022 at 16:46
  • Your if conditions do not terminate the useEffect() function. So for example, if wynik is 1000 your code will execute setPrice(priceforextrashop) then setPrice(priceforegoodshop) then setPrice(priceforstandardshop) and finally setPrice(priceforsmallshop). Consider adding else controls or inserting return statements.
    – bosco
    Commented Apr 3, 2022 at 22:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.