Open fahad19 opened 6 years ago
Just started using frintjs in an application I am building and while I like this proposal on the surface, how would the following translate to this new approach?
Basic idea being that I need grab the id
from props$
so that I can look up something on my store.
export default observe(function (app, props$) {
const store = app.get('store');
return streamProps({
id: null,
counter: -1
})
.set(
Observable.combineLatest(
props$,
store.getState$()
),
([ props, state ]) => {
const { id } = props
const { counter } = state;
return counter[id];
}
)
.get$();
})(renderFn);
@dremonkey: glad to know you are using it!
Something like this should help you reach your desired result:
import React from 'react';
import { compose } from 'frint-props-react';
import { withDefaults, withObservable } from 'frint-props';
import { distinctUntilChanged, map } from 'rxjs/operators';
function MyComponent(props) {
const { yourOwnPropKey } = props;
return <p></p>;
}
export default compose(
withDefaults({
yourOwnPropKey: null,
}),
withObservable(
(app, props$) => props$.pipe(
// let's only stream further if props passed from parent have changed
distinctUntilChanged(),
// we have latest props, and we stream the store's state
props => app.get('store').getState$().pipe(
// before passing state further, we extract only a portion of it
map(state => state.counter[props.id]),
),
// we have what we need, and shape it to a props-compatible object
desiredValue => ({
yourOwnPropKey: desiredValue,
}),
),
),
)(MyComponent);
You could also use observe
higher-order component directly:
export default observe((app, props$) => {
return props$.pipe(
distinctUntilChanged(),
switchMap(
props => app.get('store').getState$().pipe(
map(state => ({
yourOwnPropKey: state.counter[props.id]),
}),
)
),
);
})(MyComponent);
@fahad19 That's very helpful. Thank you!
Background
observe
higher-order component that is shipped fromfrint-react
streamProps
Introducing
frint-props
We also developed
frint-props
andfrint-props-react
recently, which is done in a separate repository here: https://github.com/frintjs/frint-propsThe features that they provide makes existing
streamProps
redundant.We can of course have a safe migration path for this.
Examples
Same result, one with
streamProps
, the other withfrint-props
.With
streamProps
With
frint-props
Further reading