1

There is a list l:

l = [
 ["a","b"],
 ["c","d"]
]

I want to make a SelectOption field dynamically with this list for which I write:

            <Field
              name="successData.val"
              render={({ field: { name } }) => (
                <Column>
                  <Select
                    onChange={value => setFieldValue(name, value)}
                    placeholder="dummy"
                    width={300}
                  >
                    {l.forEach(key => (
                      <SelectOption
                        value={key[0]}
                        label={key[1]}
                      />
                    ))}
                  </Select>
                </Column>
              )}
            />

But when I click the drop down button, the list doesn't appear, just the upper borderline of the box gets highlighted, it seems the drop down is getting generated, but not correctly.

Can anyone please suggest what might be wrong in this implementation?

1 Answer 1

5

Use map()

{l.map(key => (
  <SelectOption
    value={key[0]}
    label={key[1]}
  />
))}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks!, it worked, but why was it not working with forEach?
map returns a new array of the return values (which React then renders by rendering each constituent component in order), whereas forEach ignores the return value of the function passed in (using it simply for any "side effects"). So l.forEach(...) in your case, where the function doesn't have any side effects, is no different from undefined. (And of course you shouldn't do anything within render that has side effects.)
just another doubt, after selecting one of the options, its' not showing in the input box, the value still remains as the placeholder value, that would be a problem with onChange function?
setFieldValue is defined as setFieldValue: PropTypes.func.isRequired in props
@Saurabh Guessing you could make it a new post, with specific description which may help others to find your problem quickly

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.