javascript - Method Expression is not of function type (equals on a state propertyvalue) - React JS - Stack Overflow

I'm currently trying to do conditional statements which allow me to display the divs according to

I'm currently trying to do conditional statements which allow me to display the divs according to the User's role. First I call for the role and set it to the state value.

The sets are fine as I can view in the dev tools console. However when I try to do the following conditional check on a constant which is a string:

props = {
     subRole = ''
}


{(!this.state.AdminRole && subRole.toString() === READ_ONLY (
       //div goes here
))}

Then I get:

  Object(...) is not a function

on the subRole.toString() === READ_ONLY check and Webstorm is telling:

  method expression is not of function type

I'm currently trying to do conditional statements which allow me to display the divs according to the User's role. First I call for the role and set it to the state value.

The sets are fine as I can view in the dev tools console. However when I try to do the following conditional check on a constant which is a string:

props = {
     subRole = ''
}


{(!this.state.AdminRole && subRole.toString() === READ_ONLY (
       //div goes here
))}

Then I get:

  Object(...) is not a function

on the subRole.toString() === READ_ONLY check and Webstorm is telling:

  method expression is not of function type
Share Improve this question edited Aug 26, 2018 at 9:14 Timisorean 1,5167 gold badges22 silver badges31 bronze badges asked Aug 26, 2018 at 8:59 RollingStonesRollingStones 331 gold badge1 silver badge4 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 1

You have declared subRole incorrectly. In object notation we use : for keys.

Please do the following changes first:

props = {
     subRole : ''
}

If you want to initialize default props:

// for example your ponent is App.js

App.defaultProps = {
  roles : {
    subRole: 'c'
  }
};

Later you can access this in your ponent like:

this.props.roles.subRole

This is the remended way but usually we don't use default props, instead we are more concerned with the ponent's default state.

READ_ONLY is being called as a method which is why you get the error. Add && before rendering element:

{(!this.state.AdminRole && subRole.toString() === READ_ONLY && (
       //div goes here
))}

Also, Fix =

props = {
     subRole : ''
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745235948a4617892.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信