calculation of increment value in react js

2950 views javascript
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.

answered question

I'm confused? You just want to get (20 - value)%?

yes @JermahlWhite

so if the value is 12 what do you want the function to return? because otherwise it will return false

If it only returns when it's 12, you can just return {width:0.08} whenever it's 12 instead of (20 - value)/100

3 Answers

3

You can't calculate if it's a string. '' makes 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 MDNI don't understand what this has to do with react.

I think what you may want is

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

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

posted this
1

Try this

width: (20 - value) + ‘%’

posted this
4

You need something like below

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

posted this

Have an answer?

JD

Please login first before posting an answer.