Comments (5)
@rileyatodd that solution will work, but you will have problems when you decide to stop render {count} value into DOM and still updates it in the document.title.
@blacktaxi react hooks allow us to use a simpler solution then HOC:
function useSubscriptionTo(obs: Observable<unknown>): void {
React.useEffect(
() => {
const subs = obs.subscribe()
return () => subs.unsubscribe()
},
[obs]
)
}
const setTitle = count => document.title = `You clicked ${count} times`
const Counter = () => {
useSubscriptionTo(count.pipe(tap(setTitle)))
return <F.p>You clicked {count} times</F.p>
}
from focal.
For this particular example, I think it would be better to just use an F.div
like this:
import { F } from '@grammarly/focal'
// ...
const Counter = () => {
const count = store.count
return (
<F.div>Count: {count}</F.div>
)
}
I would appreciate if you could provide some other examples so I could get a sense of what kind of problem you are having.
from focal.
I guess I'm not really having any problems, just like the way hooks look 😅
But one use case could be using the result from useAtom in another hook, for example for updating the page title like they do at https://reactjs.org/docs/hooks-effect.html
import React from "react"
import { Atom, useAtom } from "@grammarly/focal"
// ...
const Counter = () => {
const count = useAtom(count)
useEffect(() => {
document.title = `You clicked ${count} times`
}, [count])
return <p>You clicked {count} times</p>
}
(although that's probably a terrible use case)
from focal.
That's actually a pretty good use case, although not very common. There is an easy way to do this kind of thing just by subscribing to the atom observable:
count.subscribe(x => {
document.title = `You clicked ${x} times`
})
The problem with this is that there's no clean up – we never unsubscribe from the observable. You can deal with this by having a special component that will manage the lifecycle of the side effect subscription. It's quite simple and could look like this:
class WithEffect extends React.Component<{ effect: () => Subscription }> {
private _sub: Subscription | null = null
private _unsubscribe() {
if (this._sub) {
this._sub.unsubscribe()
this._sub = null
}
}
componentDidMount() {
if (this.props.effect) {
this._sub = this.props.effect()
}
}
componentDidUpdate() {
if (this.props.effect) {
this._unsubscribe()
this._sub = this.props.effect()
}
}
componentWillUnmount() {
this._unsubscribe()
}
render() {
return React.Children.only(this.props.children)
}
}
// ...
const Counter = () =>
<WithEffect effect={() => count.subscribe(x => `You clicked ${x} times`)}>
<F.p>You clicked {count} times</F.p>
</WithEffect>
Unfortunately this kind of utility component is not currently included in Focal.
from focal.
I think you could avoid managing the subscription yourself by leveraging the tap
operator for the side effect.
const setTitle = count => document.title = `You clicked ${count} times`
const Counter = () =>
<F.p>You clicked {count.pipe(tap(setTitle))} times</F.p>
from focal.
Related Issues (20)
- Set state in state.subscribe() HOT 6
- How to integrate third-party React components? HOT 3
- Quick update of atom with a previous value is ignored HOT 5
- No "empty observable" warning when using a single empty Observable prop HOT 2
- Fix `instanceof Observable` check HOT 1
- Problem with .view and unsubscribe HOT 1
- [Questions] React Ecosystem Support: CRA & React Native? HOT 15
- Replace regex in Lens.prop with Proxy HOT 2
- Can't build a project HOT 2
- Typescript error with strictNullChecks HOT 3
- Any CRUD example?
- Uncaught TypeError: f.a.key is not a function HOT 4
- Publish Atom/Lens-only version HOT 5
- Classes in Atoms lose their functions after modification
- Do not log on production HOT 1
- Persistence
- Support for React 17 HOT 2
- Property 'lens' does not exist on type 'ReadOnlyAtom<State>' HOT 1
- yarn audit reports malware HOT 1
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 focal.