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.

Computed properties

See original GitHub issue

One thing I’ve wanted a couple of times in React is having computed properties, something vaguely like Ember’s computed properties:

fullName: function() {
    var firstName = this.get('firstName');
    var lastName = this.get('lastName');

    return firstName + ' ' + lastName;
}.property('firstName', 'lastName')

In my app one case where this would be useful is where one prop is a string that gets parsed into a more elaborate object before being displayed. Currently I’m doing the parsing in componentWillMount and componentWillReceiveProps and then setting state, which works pretty well, but perhaps there’s a nicer way we can provide.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
zpaocommented, Oct 12, 2018

@andreypopp I do too. The only downside is that we’ll re-evaluate that function even if this.props.firstName & this.props.lastName don’t change. If that function is doing something more complex than just string concat, it could get expensive. The upside of computed properties is that you can expire values when you know the dependents change. The downside is observing changes and expiring caches.

Generally we would suggest what @sophiebits is doing, though the fact that there’s no single lifecycle method to catch both initial props and update is annoying.

3reactions
andreypoppcommented, Jul 29, 2013

I use regular functions for this with dependencies from props or state

fullName: function() {
    var firstName = this.props.firstName;
    var lastName = this.props.lastName;

    return firstName + ' ' + lastName;
}

when props or state changes fullName reevaluated if it’s called from render() and UI is updated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Computed Properties - Vue.js
Think of a computed property as declaratively describing how to derive a value based on other values - its only responsibility should be...
Read more >
What is a Computed Property in Swift? - SwiftLee
A Computed Property provides a getter and an optional setter to indirectly access other properties and values. It can be used in several...
Read more >
What is a computed property? - free Swift 5.4 example code ...
Swift offers us two kinds of property: a stored property is one that saves a value for use later, and a computed property...
Read more >
Computed Properties - The Object Model - Ember Guides
What are Computed Properties? In a nutshell, computed properties let you declare functions as properties. You create one by defining a computed property...
Read more >
VueJS - Computed Properties - Tutorialspoint
VueJS - Computed Properties, We have already seen methods for Vue instance and for components. Computed properties are like methods but with some...
Read more >

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