0

I tried out this code. Everything seems fine to me but compiler say's onDismiss is undefined? Please help me out on this one....

import React, { Component } from 'react';

const list=[{title:'React',id:0},{title:'Redux',id:1}];

class App extends Component {

    constructor(props) {
    super(props);
    this.state={list,};
    this.onDismiss=this.onDismiss.bind(this); }

    onDismiss(id){

    const isnotid=item=>item.id!==id;
    const uplist=this.state.list.filter(isnotid);
    this.setState({list : uplist});
  }
    render() {

    return( <div className="App">
         {this.state.list.map(item=>
         <div key={item.id}>
         <span>
          {item.title}
         </span>
         <button onClick={()=>onDismiss(item.id)} type="button">
          Dismiss
         </button>
         </div>)}</div>);
    }
}
export default App;

It is a simple app that displays a list. A dismiss button is used to remove any unneeded content. Basically, I am just practicing Reactjs but don't have the clue about why my Ondismiss( The function to change content by updating component state) function is Undefined as described by compiler. Fixed typo's and little mistakes but it onDismiss function still remains undefined!

3
  • 1
    call it as this.onDismiss() Commented Jul 12, 2018 at 20:16
  • It says onDimiss in the example code. Commented Jul 12, 2018 at 20:24
  • dude corrected it but still it stays undefined! Commented Jul 12, 2018 at 20:29

1 Answer 1

1

replace

<button onClick={()=>onDismiss(item.id)} type="button">

with

<button onClick={()=>this.onDismiss(item.id)} type="button">
Sign up to request clarification or add additional context in comments.

5 Comments

thanks Yadav, but now it say's "TypeError: Cannot read property 'bind' of undefined"
@BilalKazmi You have written onDimiss in your question.
your function definition has a typo error. check onDismiss method spelling. upvote to @Tholle
@BilalKazmi With Manoj's fix, and with the typo fixed, it will work.
it still says 'undefined'?

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.