1
class ProjectFirestoreData extends Component {
    constructor(props) {
        super(props);
        this.userDelete = this.userDelete.bind(this)
    }
    userDelete(authorId){
        console.log(authorId)
    }
    render() {
        let {firestore} = this.props;
        if(!Array.isArray(firestore) || !firestore.length ){
            return (
                    <div>
                        <h1>Loading</h1>
                    </div>
                )
        }
        else{
            return (
                    <div className="col-md-8">
                        <div className="row">
                            {
                                firestore.map(function(index, elem) {
                                    return (
                                        <div className="col-md-6 p-3" key={index.name}  >
                                            <div className="card">
                                              <img className="card-img-top" src="..." alt="Card" />
                                              <div className="card-body">
                                                <h5 className="card-title text-dark">AuthorName is :  {index.authorFirstName} </h5>
                                                <p className="card-text text-dark">Some quick example text to build on the card title and make up the bulk of the card's content.
                                                </p>
                                                <Link to={`/userdetails/${index.authorId}`} className="btn btn-primary">Show Full Data</Link>
                                                <button type="button" class="btn btn-dark" onClick={() => this.userDelete(index.authorId)}>Dark</button>
                                              </div>
                                            </div>
                                        </div>

                                        )
                                })
                            }


                        </div>
                    </div>
                )
        }
    }

}

onclick event is working after the array.map , but it is not working under the array.map is there any way of achieving it ? i have tried adding

<button type="button" class="btn btn-dark" onClick={() =>this.userDelete(index.authorId)}>Dark</button>

after array.map and it is working fine . but under array.map it is not working . the error says

TypeError: Cannot read property 'userDelete' of undefined

2 Answers 2

5

By passing a regular function to the map you lose the this context (a regular functions' this referrers to the current function), and since your userDelete function is outside of the map callback function userDelete is undefined, if you change the map callback function to an arrow function you retain the this scope of the class therefore userDelete is still in scope.

Change firestore.map(function(index, elem) { to firestore.map((index, elem) => { this should keep the proper this scope.

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

2 Comments

awesome ... solution was very easy but i was not getting the logic of this .. thanks :) it is solved now
See my update for a brief explanation of the logic.
0

onclick event inside map:

 <div className={styles.file_content_show}>
                            {this.state.fileinformation.map((item)=>{ 
                                    return (
                                      
                                      <p className={styles.file_name_show}>{item.file_name}
                                      <span className={styles.file_size_date}> ({item.file_size} | {item.file_modified_date}) <img src={attachmentdelete_logo} className={styles.attachmentdelete_logo} onClick={this.deleteattachment.bind(item.file_size)}/></span>
                                      </p>
                                    );    
                                    
                                  })}
                                 
                                  
                                </div>

Function is this:

@autobind
private deleteattachment(file){
  var a=file;
}

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.