Computed properties
See original GitHub issueOne 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:
- Created 10 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top 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 >
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top 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
@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.
I use regular functions for this with dependencies from
props
orstate
when
props
orstate
changesfullName
reevaluated if it’s called fromrender()
and UI is updated.