0

I have a component that needs to load a certain item according to a props it receives from the parent component.

const Contract = ({ savingsFactors, isContract }) => (
    {isContract ? (
        <PutField
        label={label}
        placeholder={placeholder}
        onBlur={...}
        items={savingsFactors === 'true' ? FORM_VALUES : FORM_VALUES_NORMAL}
        />        
    ) : (
        <PutField
        label={label}
        placeholder={placeholder}
        onBlur={...}
        items={savingsFactors === 'true' ? ANOTHER_FORM_VALUES : ANOTHER_FORM_VALUES_NORMAL}
        />
    )}
);

The only thing that changes is the values ​​inside the items.

These values ​​are objects with arrays:

const FORM_VALUES = [
    { name: 'some name', value 'some value' },
    { name: 'some name', value 'some value' },
]
const FORM_VALUES_NORMAL = [
    { name: 'some name', value 'some value' },
    { name: 'some name', value 'some value' },
]
const ANOTHER_FORM_VALUES = [
    { name: 'some name', value 'some value' },
    { name: 'some name', value 'some value' },  
]
const ANOTHER_FORM_VALUES_NORMAL = [
    { name: 'some name', value 'some value' }
]

Is there any way I don't need to repeat these two blocks of code?

4
  • why not put the condition inside your items prop? Commented Jun 2, 2021 at 20:40
  • I tried, but it didn't accept if or ternary chaining Commented Jun 2, 2021 at 20:41
  • can you show us what you tried? it can definitely work that way Commented Jun 2, 2021 at 20:42
  • 1
    you can also define a variable items above the component, figure out what it needs to be then pass it items={items} Commented Jun 2, 2021 at 20:42

2 Answers 2

1

I always like to be as explicit as I can:

const Contract = ({ savingsFactors, isContract }) => {
  const isSavingsFactors = savingsFactors === 'true';
  const formValues = isSavingsFactors ? FORM_VALUES : FORM_VALUES_NORMAL;
  const anotherFormValues = isSavingsFactors ? ANOTHER_FORM_VALUES : ANOTHER_FORM_VALUES_NORMAL;
  const items = isContract ? formValues : anotherFormValues;

  return <PutField
     label={label}
     placeholder={placeholder}
     onBlur={...}
     items={items}
  />
};
Sign up to request clarification or add additional context in comments.

Comments

1
{
        let itemsToUse = savingsFactors === 'true' ? FORM_VALUES : FORM_VALUES_NORMAL;
        if (!isContract) {
            itemsToUse = savingsFactors === 'true' ? ANOTHER_FORM_VALUES : ANOTHER_FORM_VALUES_NORMAL
        }
        return <PutField
            label={label}
            placeholder={placeholder}
            onBlur={...}
            items={itemsToUse}
        />
   
    }

1 Comment

I need to do this verification inside of const Contract?

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.