26

If array of user_list.length == 0, the "clear user" button should be disabled. When I am calling this.disabledButton function, it throws an error saying

Type '() => boolean' is not assignable to type 'boolean'

Below is my logic.

<section>
     <Button
        id='clear-user'
        disabled={this.disabledButton}
        onClick={this.onClearAllUserButton}>
        {'Clear User List'}
     </Button>
</section>

Function defined:

 private disabledButton= (): boolean => {
    if (this.props.user_list.length == 0) {
      return true
    }
    return false
  }

Is my calling wrong?

2
  • 2
    disabled prop accepts a boolean value, disabledButton is a function that returns a boolean value. A function is not assignable to a boolean. Commented Feb 28, 2020 at 22:49
  • A function returning a boolean is not assignable to a boolean. Seems logical. Perhaps the function was to be invoked such that the result could be assigned.. Commented Feb 28, 2020 at 22:49

1 Answer 1

47

You're passing a function that returns a boolean (type () => boolean) to a property that expects a boolean (type boolean). You just have to actually execute your function.

disabled={this.disabledButton()}

This error message tells you everything:

"Type '() => boolean' is not assignable to type 'boolean'"

() => boolean is the function you are trying to assign, and boolean is the type it's expecting. It's up to you to notice what those two types are and figure out how to make them match.

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.