1

Why in the following code Delete button click does not hit the delete method? What i am missing important thing? I am new to learn React.js

delete(e) {
    console.log('Deleted');
}

static renderCatTable(Categories) {
    return (
        <table className='table table-striped'>
            <thead>
                <tr>
                    <th>Code</th>
                    <th></th>

                </tr>
            </thead>
            <tbody>
                {Categories.map(category =>
                    <tr key={category._id}>
                        <td>{category.code}</td>
                        <td><button onClick={this.delete} className="btn btn-danger">Delete</button></td>                        
                    </tr>

                )}
            </tbody>
        </table>
    );
}

I have defined the binding inside the constructor

this.delete = this.delete.bind(this);

The render function is given below.

render() {
    let contents = this.state.loading
        ? <p><em>Loading...</em></p>
        : Category.renderCatTable(this.state.Categories);
    return (
        <div>         
            {contents}
        </div>
    );
}

1 Answer 1

3

Because that render method is static. By definition, static methods cannot access an instance variable. You should remove that modifier if possible and it should work.

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

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.