0

I got my data from firebase, looped through it and displayed it on the frontend. Now I am trying to get the refs value of the already displayed value when I click on it. For example, when I click on Dino, i should be able to see the value 'Dino' on my console tab of Chrome browser.

Here is a link to the picture of the array list displayed on react frontend

<ul>
    <li onClick={this.handleSubmit}>        
        {               
            Object.keys(this.props.group).map(function(keyName, keyIndex) {
                return(
                    <div key={keyIndex}>{keyName}</div>
                )
            })
        }
    </li>
</ul>
1
  • 1
    Some code that reproduces the problem would be more useful than a picture here. Could you try setting up a demo in jsfiddle? Commented Jul 7, 2017 at 19:29

2 Answers 2

1

Assuming that those "dino, magic & muu" are the keys in this.props.group you need to add an onClick handler to that div, so:

<div key={keyIndex} onClick={() => console.log(keyName)}>{keyName}</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You'll need to bind a click event to each div within the li. I would actually rewrite it like so:

 <ul>    
    {               
        Object.keys(this.props.group).map( (keyName, keyIndex) => {
            return(
                <li key={keyIndex} onClick={this.handleSubmit.bind(this, keyName}>  
                    {keyName}
                </li>
            )
        })
    }
</ul>

Then your handleSubmit function will get the keyName as a param:

handleSubmit(keyName){
    // Do something...
}

1 Comment

Thank you very much, the this keyword was nightmare for me. Now i have seen my mistake

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.