0

I have this component, that inherits a props with a value of string 'A','B','C' depending on what is getting selected at a parent element the point being is the props.options either 'A' or 'B' or 'C' then I want to leverage this incoming props to be able to select a local option list from options array.. depending on what is being passed down instead of passing the whole options arrays from the parent down to child component... I am trying to use template literals but it's not working any local work around so I could render the options array depending on incoming string props? for this particular case.

// incoming props  string of A or  B or  C depending on what is getting seleted at parent element ! 

function OptionAcordingToProps(props) {
 
 const optionsA = ['x', 'x', 'x'];
 const optionsB = ['y', 'y', 'y'];
 const optionsC = ['z', 'z', 'z'];
 
  return (
    <div>
      <form onSubmit={doSmth}>
        <NativeSelect>
             {`options${props.options}`?.map((option) => (
            <option>{option}</option>
          ))}
        </NativeSelect>
        </form>    
       </div>
  );
}

1 Answer 1

3

Use an object as a map:

function OptionAcordingToProps(props) {
 
  const options = {
     A: ['x', 'x', 'x'],
     B: ['y', 'y', 'y'],
     C: ['z', 'z', 'z'],
  };
 
  return (
    <div>
      <form onSubmit={doSmth}>
        <NativeSelect>
             {options[props.options]?.map((option) => (
            <option>{option}</option>
          ))}
        </NativeSelect>
        </form>    
       </div>
  );
}
Sign up to request clarification or add additional context in comments.

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.