Update on props change
See original GitHub issueHi,
I can’t figure out what I am doing wrong. Can I show/hide UI element depending on object? here is some sample of code:
// ability.js
import { AbilityBuilder } from '@casl/ability'
function subjectName(item) {
if (!item || typeof item === 'string') {
return item
}
console.log('Type is: ', item.__type);
return item.__type
}
export default AbilityBuilder.define({ subjectName }, can => {
can(['update', 'delete'], 'Account', { creator: 'John', role: 'Manager'})
})
then in my component I do:
import React, {Component} from 'react';
import Can from "./Can";
class MyComponent extends Component {
state = {
user: '',
Perm: {
creator: 'Jeff',
role: 'Worker',
__type: 'Account',
}
}
clicked = () => {
this.setState({Perm: {
creator: 'John',
role: 'Manager',
__type: 'Account',
}
})
}
render () {
console.log(this.state.Perm);
return (
<div>
<h1>Permission</h1>
<div>{this.state.Perm.creator}</div>
<Can do='update' on={this.state.Perm}>
<button>Modify</button>
</Can>
<button onClick={this.clicked}>Change</button>
</div>
)
}
}
export default MyComponent;
Jeff user indeed does get switched for John, so the component should be shown. I probably failed to understand something. The main reason why I am asking, is the fact that I will be getting roles from Redux, therefore I want to update my Navigation once user has logged in - depending on what role comes back from REST API
Appreciate any help!
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Reacting to Prop Changes in a React Component | Pluralsight
When building a component using React there is often a requirement to create a side effect when one of the component props changes....
Read more >Updating state when props change in React | bobbyhadz
To update state when props change in React, pass the props as dependencies to the `useEffect` hook. Every time the props change, the...
Read more >Re-render React component when prop changes
An alternative solution for re-rendering a component on props change is to ... Only use an updated prop to update the state in...
Read more >Why React Child Components Don't Update on Prop Changes
In particular, a common problems is to wonder why a child component doesn't update when its prop changes. In React, it's a common...
Read more >How to: Updating state on prop changes | by Avery Duffin
The second thing to notice is that it returns a new state object. The way you can update the state on changes is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Дуже дякую!
Works as expected!!!
Fixed in
@casl/react@0.4.0
. Thanks for using CASL!