question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update on props change

See original GitHub issue

Hi,

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:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
chickenandsnakecommented, Apr 23, 2018

Дуже дякую!

Works as expected!!!

1reaction
stalniycommented, Apr 23, 2018

Fixed in @casl/react@0.4.0. Thanks for using CASL!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found