2

I found many related questions regarding inline style of ReactJS but non suitable to mine.

I need to change the animation speed(1,3,4,6,7 are my animation speeds) in UI as per the API values (which changes dynamically)

I tried the following way

<img className="fan1" src={fan1} style={{animation: "spin "`${2}`"s linear infinite" }}></img>    
<img className="fan1" src={fan1} style={{animation: "spin ${2}s linear infinite" }}></img>
<img className="fan1" src={fan1} style={{animation: `spin {2}s linear infinite` }}></img>
<img className="fan1" src={fan1} style={{animation: {spin {2}s linear infinite} }}></img>

I'm not sure how to acheive this. Please help me out.

2
  • 2
    Every single one of those is invalid or incorrect Javascript string syntax lol. Read this carefully and check examples: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… and revisit basic Javsacript string syntax Commented Sep 12, 2020 at 10:15
  • @Jayce444 post that link, i'll accept Commented Sep 12, 2020 at 10:22

2 Answers 2

1

This works for me

<img className="fan1" 
      src={fan1}
      style={{ animation: `spin ${5}s linear infinite` }}
      alt="fan"
  ></img>
Sign up to request clarification or add additional context in comments.

Comments

1

Your main problem is that the style strings are not valid JavaScript. May I suggest reading up on the syntax on template literals. If you change that I suspect it should then work. So your code should look something like this,

<img className="fan1" src={fan1} style={{animation: `spin ${2}s linear infinite` }}></img>    
<img className="fan1" src={fan1} style={{animation: `spin ${2}s linear infinite`  }}></img>
<img className="fan1" src={fan1} style={{animation: `spin ${2}s linear infinite`  }}></img>
<img className="fan1" src={fan1} style={{animation: `spin ${2}s linear infinite`  }}></img>

You loud then replace the ${2} with a link to the state that is constantly being updated.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.