0

I'm creating a component for selecting an input based on available options in my courses object.

I'm currently getting this error: Uncaught TypeError: Cannot read property 'map' of undefined

Here is my code:

const SelectInput = ({name, label, onChange, defaultOption, value, error, options}) => {
    return (
        <div className="form-group">
            <label htmlFor={name}>{label}</label>
            <div className="field">
                {/* Note, value is set here rather than on the option - docs*/}
                <select name={name} value={value} onChange={onChange} className="form-control">
                <option value="">{defaultOption}</option>
                {options.map((option) => {
                    return <option key={option.value} value={option.value}>{option.text}</option>;  
                })}
                </select>
                {error && <div className="alert alert-danger">{error}</div>}
            </div>
        </div>
    );
};

Anyone knows the reason?

2
  • why do you need to wrap your parameters as an object {} ? Commented Jun 29, 2016 at 9:55
  • 1
    @BagusTrihatmaja it is object destraction Commented Jun 29, 2016 at 10:01

1 Answer 1

1

You expect that options are an array but you provide undefined options prop, so there is no map method for undefined

Provide not undefined options or try this:

const SelectInput = ({name, label, onChange, defaultOption, value, error, options = []}) => {
  ...
}
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.