[英]Conditional rendering of showing / hiding a button - ReactJs
I am trying to hide a menu button if there is . 我试图隐藏菜单按钮,如果有的话。 Baiscly if the user is not allowed to see the menu i receive an empty rol from the token ( basicly
rol : ['']
, otherwise if it is allowed is rol : ['_DECLARATION']
. I have tried with a conditional rendering but maybe i am mistaken of my methods that i use. If somebody could give a hand would be very nice . 如果用户不被允许看到菜单,我会从令牌中收到一个空的rol(基本上是
rol : ['']
,否则如果允许的话是rol : ['_DECLARATION']
。我试过条件渲染但是也许我误解了我使用的方法。如果有人能伸出援助之手会非常好。
constructor(props) {
super(props);
this.state = {
roles: ''
}
async componentDidMount() {
const base64Url = token.split('.')[1];
const decodedValue = JSON.parse(window.atob(base64Url))
const roles = decodedValue.roles;
render() {
const { roles } = this.state
return (
{ roles
? <Button className="emptyRollButtonDec"/>
: <Button
onClick={this.changeActiveComponent
style= {{
background: branding.color.colorPrimary,
color: branding.color.colorTextPrimary
}}>
Declaration
</Button>
}
I dont know and i dont understand where i am wrong . 我不知道,我不明白我错在哪里。
You've defined roles
in state as a zero length string. 您已将状态中的
roles
定义为零长度字符串。 This means that it is defined, not undefined, when you destructure it from the state in your render method. 这意味着当您从render方法中的状态对其进行解构时,它是定义的,而不是未定义的。 This means that you can't use your existing ternary check because
roles ? something : somethingelse
这意味着您不能使用现有的三元检查,因为
roles ? something : somethingelse
roles ? something : somethingelse
checks to see if roles
is defined or not. roles ? something : somethingelse
检查是否定义了 roles
。
To correct this check the length of roles
instead. 要更正此问题,请检查
roles
的长度 。 If it is zero return the empty button, otherwise show the active button: 如果为零则返回空按钮,否则显示活动按钮:
!roles.length ? showEmptyButton : showActiveButton
First, be sure to update the roles
state of your component using setState()
. 首先,确保使用
setState()
更新组件的roles
状态。 In the case of your component, you could do that in the componentDidMount()
life-cycle hook: 对于组件,您可以在
componentDidMount()
生命周期钩子中执行此操作:
/* Remove async from method signature as it's not needed */
componentDidMount() {
const base64Url = token.split('.')[1];
const decodedValue = JSON.parse(window.atob(base64Url))
const roles = decodedValue.roles;
/* Very important - this will trigger a re-render for your
component which will ensure that the subsequent render is
done so with the updated roles state in effect */
this.setState({ roles : roles });
}
Next, you need to ensure that the your conditional role-based rendering is based on boolean represnetation of the roles
state of the component. 接下来,您需要确保基于条件角色的条件渲染基于组件
roles
状态的布尔重新执行。 As I understand from your question, the empty roles button is shown if the roles
state is either ''
or ['']
: 据我所知,如果
roles
状态为''
或['']
则会显示空角色按钮:
render() {
const { roles } = this.state;
/* If roles is falsey, or first element of roles array
is falsey then show empty button */
const isEmptyRole = !roles || !roles[0];
return isEmptyRole ?
<Button className="emptyRollButtonDec" /> :
(<Button
onClick={this.changeActiveComponent}
style={{
background: branding.color.colorPrimary,
color: branding.color.colorTextPrimary
}}>Declaration</Button>) :
;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.