0

trying to do a nested if statement or if else if statement in jsx. essentially if there is 1 input it has the label of Primary, if there are 2 inputs, the first one has label primary and second one has label of Secondary and if there are more than 2 the first has label as Primary, second as Secondary and 3 going forward have no label.

Currently my code puts label Primary on the first and Secondary on the second but as soon as I add a third it erases all labels. Any ideas would be awesome.

  render() {
    const { lowerType, currentPerson, contact, index, type } = this.props;
    const isPrimary = index === currentPerson.getIn(['primaries', lowerType]);
    // const secondaryLabel = <label><b>Secondary</b></label>;
    const label = <label><b>{ isPrimary ? 'Primary' : 'Secondary' }</b></label>;
    return (
      <div className='field main-item'>
        { currentPerson.get(`${lowerType}s`).size <= 2 && label }
        <Input
          value={contact}
          onChange={this.handleInputChange}
          placeholder={`Enter ${type === 'Email' ? 'an' : 'a'} ${type.toLowerCase()}`}
          type={type === 'Email' ? 'text' : 'tel'}
          style={inputRight}
        />
      </div>
    );
  }

2
  • At which line is the desired behavior not happening? Commented Jul 28, 2017 at 14:41
  • const label = <label><b>{ isPrimary ? 'Primary' : 'Secondary' }</b></label>; return ( <div className='field main-item'> { currentPerson.get(${lowerType}s).size <= 2 && label } Commented Jul 28, 2017 at 15:07

1 Answer 1

1

You are using the below condition, which will return the label only if currentPerson.get(${lowerType}s).size is less than or equal to 2.

{ currentPerson.get(`${lowerType}s`).size <= 2 && label }

If you want don't want to erase the label, or give another text it would be better if instead of using && you use tertiary operator over here.

{ currentPerson.get(`${lowerType}s`).size <= 2 ? label : 'third label' }
Sign up to request clarification or add additional context in comments.

7 Comments

this seems closer to what I want to do, but it still removed the Primary and Secondary label from the first 2 inputs. What I am aiming for is <label>Primary</label> <input/> <label>Secondary</label> <input/> <input/> <input/> .... so any inputs added after the 2nd one do not have a label.
then you should just put { label }. Why you are using the condition if you dont want to remove the label ??
Where do I put the { label } ? The condition I thought was necessary because there is always 1 input which will have the label of Primary, when a second input is added, it is given the label of Secondary and when a third, fourth, fifth, input is added it just falls under the same label as the Secondary rather than have their own labels.
If you want more control, you can just use a function which returns the JSX and call it inside {}.
oh really? How would that be done? Sorry, newer to this JSX react/redux stuffs.
|

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.