Comments (4)
@gemma-ferreras the solution I've ended up w/ looks like:
const useStream = <T, R>(
project: (stream$: Observable<T>) => Observable<R>,
data: T,
): R | undefined => {
const prev = useRef<T>()
const stream$ = useRef(new Subject<T>())
const emit = useRef<R>()
const synchronous = useRef(true)
const [_, rerender] = useState(false)
useLayoutEffect(() => {
const subscription = stream$.current.pipe(project).subscribe({
next: (next) => {
emit.current = next
if (!synchronous.current) {
rerender((prev) => !prev)
}
}
})
stream$.current.next(data)
return () => subscription.unsubscribe()
}, [])
synchronous.current = true
if (prev.current !== data) {
emit.current = undefined
stream$.current.next(data)
}
prev.current = data
synchronous.current = false
return emit.current
}
It's a little bit wordier than I'd hoped, but essentially subscribes to a stream for the lifecycle of the component, while ensuring that synchronous emits don't render twice. To use:
export const Widget: SFC<{}> = () => {
const [channel, setChannel] = useState('friend-list')
const selectChannel = useCallback(({ target: { value } }) => setChannel(value), [])
const next = useStream((stream$) => stream$.pipe(
switchMap(() => interval(500).pipe(
startWith(-1),
scan<number, number[]>((data) => [...data, Math.floor(Math.random() * 10)], []),
take(4),
)),
), channel)
return el('div', null,
el('div', null,
el('select', { value: channel, onChange: selectChannel },
el('option', { value: 'friend-list' }, 'Friends'),
el('option', { value: 'enemy-list' }, 'Enemies'),
el('option', { value: 'grocery-list' }, 'Groceries'))),
el('h1', null, channel),
el('ul', null, ...(next || []).map((item, idx) => (
el('li', { key: idx }, item))))
)
}
(accidentally fat finger closed this--just reopened)
from recompose.
the author has discontinued support for recompose and recommended react hook as a replacement.
from recompose.
Understood, and I think that's a fine choice. I'm just not sure how (or even if, given the current state of the hooks API) it would be possible to recreate the functionality of mapPropsStream using hooks.
At issue is delaying the component's render from when it receives props to when the observable emits (which may be immediate or not). The current recompose implementation, and the above simplified reimplementation, essentially achieve that by using shouldComponentUpdate
. Deferring rendering is not something that's supported by hooks atm, though I suspect the suspense API is intended for this type of use case.
from recompose.
@jameslaneconkling Did you manage to recreate the functionality of mapPropsStream using hooks? I am looking for this
from recompose.
Related Issues (20)
- renderNothing should export Nothing HOT 4
- Does TypeScript 3.4 enable typing of compose HOT 1
- Remove flow types in repo and support only in flow-typed repo HOT 1
- Key advantages in Docs HOT 2
- The best way to test a component that use getContext recompose HOT 1
- Get a lifecycle componentDidMount unmounted error HOT 1
- compose hoc causes to lose typings
- Remove usage of `createFactory` HOT 4
- Why does `mapProps` remove `children`? HOT 2
- node-fetch vulnerability issue (denial of service) HOT 8
- Forked, Updated, and Published HOT 1
- fbjs dependency still present in lock file HOT 3
- Support React 17 HOT 2
- Suggest alternative to branch(), renderComponent(), renderNothing(), compose(), etc
- TypeError: Cannot read properties of undefined (reading 'apply')
- Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
- New patch required to mitigate high risk vulnerability
- Maintainers HOT 1
- This is GITHUB and not the European Parliament or NATO.
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 recompose.