4

I have some issues when im trying to reference inputs as arrays in React with Redux.

The code below maps one Panel per article in the array.

var articles = this.props.item.array.articles.map((article, index) => {
     return <Panel key={index} header={'Article ' + index}>
        <Input type='select' ref='id' label='Article' defaultValue={article.id} >
          {articles}
        </Input>
     </Panel>
})

I'm trying to construct the refs so that they're in an array format, which does not seem to be possible at the moment. Array of references. #1899

I guess i could solve this by create some sort of ref="article["+counter+"][id]"

But that is a horrible solution, and i really don't want to go down that path.

The json array below would be my desired format for the refs:

"articles": [
        {
            "_joinData": {
                "price": "100",
                "quantity": "50"
            },
            "id": "05f54207-fb6f-40b5-820e-26059a803343"
        },
        {
            "_joinData": {
                "price": "200",
                "quantity": "70"
            },
            "id": "05f54207-fb6f-40b5-820e-26059a803343"
        }
]

The price & quantity index would be 2 more inputs.

Which i've decided to not include in the code example.

A nice solution to this problem would be very appreciated.

3
  • The question is not clear. this.refs is a String-keyed Object, not a "JSON array" Commented Feb 15, 2016 at 23:20
  • No it's not and what i meant was that my desired output for the articles index would be a json array in the specified format. Commented Feb 16, 2016 at 7:52
  • How is that array related to refs? Commented Feb 16, 2016 at 14:14

1 Answer 1

2

I believe you can iterate through this.refs like an array by using Object.keys.

Ex. Object.keys(this.refs).forEach(key => func(this.refs[key]))

To run func function for each reference.

Sign up to request clarification or add additional context in comments.

1 Comment

this can be simple: Object.values(this.refs)

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.