0

i created a table in React.js, also I use Firebase for data storage, here is my table code

{tableData.map((row, index) => {
    return (
        <tr key={row + index}>
            {row.map((cell, index) => {
                return (
                    <td key={"cell_" + index} onClick={() => this.handleClick(this.state.userSurname)}>{cell}</td>
                );
            })}
        </tr>
    );
})}

and here is my this.setState:

componentDidMount() {
    db.onceGetUsers().on("child_added", snap =>{
                var username = snap.child("name").child("name").val();
                var surname = snap.child("name").child("surname").val();
               this.setState({ users: this.state.users.concat([username])});
               this.setState({ userSurname: this.state.userSurname.concat([surname])})
    })
  }

Here is my handleClick function:

  handleClick(event){
    alert(event)
}

So.. here is my problem, alert dialog displays every user surname, and what I'm trying to do is to display surname of user that is clicked in table.. how can i do that? :)

7
  • what is in tableData? Commented Apr 27, 2018 at 18:45
  • Ofcourse i forgot, in tableData are usernames Commented Apr 27, 2018 at 18:46
  • Only usernames, no surnames? Commented Apr 27, 2018 at 18:50
  • only usernames, now onClick on username i want to display clicked user surname :) Commented Apr 27, 2018 at 18:51
  • maybe instead of having two state variables for username and surname, you can have one state variable consisting of an array of user objects. Commented Apr 27, 2018 at 18:52

2 Answers 2

2

You can achieve it like this, if you don't want to change your state structure:

handleClick(i){
    alert(this.state.userSurname[i])
}

{tableData.map((row, i) => {
    return (
        <tr key={row + i}>
            {row.map((cell, k) => {
                return (
                    <td key={"cell_" + k} onClick={() => this.handleClick(i)}>{cell}</td>
                );
            })}
        </tr>
    );
})}
Sign up to request clarification or add additional context in comments.

3 Comments

I displays surname only from 1st username in array :)
Yea because you have another index when you map row.
Change the name of one of the indexes and their usage.
1

From the docs, child_added:

Retrieve lists of items or listen for additions to a list of items. This event is triggered once for each existing child and then again every time a new child is added to the specified path. The listener is passed a snapshot containing the new child's data.

To display surname of only the user that clicked in table, you need to read data once, example:

db.onceGetUsers().once('value').then(snap)=> {
var username = snap.child("name").child("name").val();
var surname = snap.child("name").child("surname").val();
});

once

Listens for exactly one event of the specified event type, and then stops listening.

more info here:

https://firebase.google.com/docs/reference/js/firebase.database.Reference#once

2 Comments

When i try to retrieve data this way, once('value', snap=>{ whole retrieved data is null :)
@jj.badweyn db.onceGetUsers().once('value').then(snap)=> { snap.forEach(childs)=> { var username = childs.child("name").child("name").val();});

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.