0

I want to pass the parameter value in style for calculate my width. my code is

function getValue(value) {
  if (value === 12) {
    return {
      width: '( 20 - value )%',
    };
  }
  return false;
}

but the width does not working. I am new to react. please help.

4
  • I'm confused? You just want to get (20 - value)%? Commented Nov 9, 2018 at 4:45
  • yes @JermahlWhite Commented Nov 9, 2018 at 4:47
  • so if the value is 12 what do you want the function to return? because otherwise it will return false Commented Nov 9, 2018 at 4:51
  • If it only returns when it's 12, you can just return {width:0.08} whenever it's 12 instead of (20 - value)/100 Commented Nov 9, 2018 at 4:57

3 Answers 3

2

You can't calculate if it's a string. Quote marks ('') make it a string. Also, % is a remainder operator. It gets the remainder of two numbers. It's similar to division. You can read more about the remainder operator on MDN I don't understand what this has to do with react, this is more just vanilla javascript.

I think what you may want is

function getValue(value) {
  if (value !== 12) { return false; }
  return {
    width: (20 - value)/100,
  };
}

You could also do the following since it only returns if the value if 12

var getValue = value => value === 12 ? { width: 0.08 } : false

This solution uses arrow functions, auto return, and ternary operators. You can read about them on mdn

Sign up to request clarification or add additional context in comments.

5 Comments

free vote from me :) thanks for calling out remainder and also linking to MDN!
I feel violated. Edited without my knowing
Haha! I helped didn't I? :)
@JohnRuddell How did you help??? I need to know how you helps so I can express gratitude
Did you see what i edited and the comment i put? To view my edit you have to click the link edited 1 hour ago
2

You need something like below. You are checking a constant value, so no need to do the arithmetic, just return 8. You can simplify the function as like below

function getValue(value) {
  // return object for style or undefined for no styling
  return value === 12 ? {'width': '8% !important' } : undefined;
}

If you were to keep the arithmetic because you wanted to just always do a calculation (for instance). Then a nicer way to write out the function would be like this.

function getValue(value) {
  return { 'width': `${20 - value}% !important` };
}

5 Comments

Why even use a second line? You could just check to see if it's 12, and if it is, return 0.08? since (20-12)% is always 8%
Agree with you :)
Free upvote because you agree with me and also because of quality. Definitely mostly because quality ;)
Free upvote and edits from me :) nice way to "think twice" ;) about the attempt!
I wrote your code..but when i use width: '7%' it shows different width and when i write width: ${20 - value}%` ` it shows different width ..i don't know what is the difference between this two codes. and here value is 13 @JohnRuddell
1

Try this

width: (20 - value) + ‘%’

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.