0

I'm sure this is a simple question, but actually I can't figure out, why it is not working. I'm still in beginner with react-redux, so please be patient with me. ;-)

I'm loading different data with react-redux. Now I want to fill a select box with the loaded values.

I can see the data is loaded correctly (props.anreden & props.benutzer). So I can be sure that the data is loaded correctly and I have error somewhere else.

enter image description here

The difference is that anreden is an obejct-array, while benutzer is an object.

When I want to fill the select box in the render() method, it fills only the data of benutzer and not of anreden:

enter image description here

This is the code of this part:

<div className="select">
    <select
        value={0}                
        className={`form-control tabable`}
    >
        <option value={0}>auswählen</option>
        <option value={this.props.benutzer.id}>{this.props.benutzer.vorname}</option>
        {this.props.anreden && this.props.anreden.length > 0 &&
        this.props.anreden.map((option) => {
            return (<option value={option.id}>{option.bezeichnung}</option>)
        })}
    </select>
</div>

Does anyone have a hint for me, why it is like this?

3
  • Are you including all those redux state values in mapStateToProps? Commented Sep 4, 2019 at 19:34
  • this.props.anreden are you sure its an array, you can verify using console.log Commented Sep 4, 2019 at 19:35
  • It is not related to Redux. Also provide what console says. Probably because you did not provide key attribute <option value={option.id} key={option.id}>{option.bezeichnung}</option> Commented Sep 4, 2019 at 19:37

1 Answer 1

3

It looks like this.props.anreden is an Object, not an array as you expect it to be. So this.props.anreden.length > 0 is always false.

Map over values. Also you need keys.

{this.props.anreden && 
    Object.values(this.props.anreden).map((option) => {
        return (<option value={option.id} key={option.id}>{option.bezeichnung}</option>)
    })}
Sign up to request clarification or add additional context in comments.

1 Comment

@dns_nx Glad I could help :)

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.