Comments (8)
Yep, It's definitely possible to add computed fields.
But the thing is you don't actually need computed states in React since updating the state re-runs the component, so you can just have a plain variable/constant that computes the value out of the state, on a given re-render
// radiaoctive-state in react
// state
const state = useRS({
count: 0
})
// computed state
const doubleCount = state.count * 2
Computed states are required on hyper-efficient frameworks that do not re-run the component when the state is updated.
Take nue.js for example - it's a reactive framework I'm working on which has the same core idea as this library
you can check out a simple nuejs app that shows usage of computed state here: https://codepen.io/MananTank/pen/NWbRzvX?editors=1010
Here's how nuejs does the "computed state" business:
// nue.js
$.count = 0
$.doubleCount = () => $.count * 2
In nuejs, A simple reactive state is a value and a computed state is a function.
when nuejs initializes the state - it runs the computed state's function in "spy mode" which detects what parts of the state is being used to calculate the computed state and then computed state subscribes to those parts - so that whenever they are updated - its value is recomputed. (here in this example
This kind of computed state logic is required in nue.js because it is template-based, you can't embed javascript expressions in it, so we have to have a computed state to allow having expressions
But in React, we write JSX, and since you can embed js expressions directly inside JSX and everytime the state is updated the component re-runs - no need to have a computed state.
Hope this answers your question ;)
from radioactive-state.
What if you only use the derived values in the state? Also, if you componentize a lot, subcomponents won't rerender as much as the root one
from radioactive-state.
Also, if derived state is useless, why mobX and React-Easy-State implements it?
from radioactive-state.
I'm not saying that derived state is useless, I'm saying it's not necessary in vanilla React, but if you are using a global state management library where a piece of state could be updated in any component - in that case, we would have to have derived/computed state.
But in the case of radioactive-state - it's not a global state-management library - it's just a local state management hook. state it produces will only be used and updated by itself (and its children - still a single root) and whenever the state is updated - the component where the state was created (root component) is rerendered - this removes the need to detect the state change and update some other piece of state.
Also, if you componentize a lot, subcomponents won't rerender as much as the root one
I'm not sure what you mean by this, subcomponents would render the same amount as the root one.
from radioactive-state.
Hmm I thought react had a internal smart cache to avoid rerender of subcomponents uselessly
from radioactive-state.
You could use React.memo for that https://reactjs.org/docs/react-api.html#reactmemo
from radioactive-state.
https://overreacted.io/before-you-memo/
from radioactive-state.
But yeah, now I understand the purpose of radioactive-state.
from radioactive-state.
Related Issues (5)
- Typo in doc HOT 1
- Adding react as a devDependency HOT 2
- destructuring the RS state object ? HOT 1
- usage with moment.js HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from radioactive-state.