reacttraining / react-broadcast Goto Github PK
View Code? Open in Web Editor NEWReliably communicate state changes to deeply nested React elements
License: MIT License
Reliably communicate state changes to deeply nested React elements
License: MIT License
I'm personally starting to prefer using render
instead of children
whenever I'm using a render prop. But that may just be me.
It should be possible to change the channel in a subscriber, but it's not implemented yet. Is this something that people need? For now I think I'll put an invariant there that prevents people from trying to change it.
Currently, if you render a <Subscriber>
out of context it'll throw an invariant. This is similar to the context API itself, where if you try to use this.context.cupcakes
outside the appropriate context you'll get an error.
What we don't have is a way for users to opt-out of the invariant when they're not rendered in the right context. This would be the equivalent of the isRequired
functionality currently available to consumers of the context API through contextTypes
.
The idea would be to add <Subscriber quiet>
to opt-out of the invariant in these situations.
The readme shows you a lot of boilerplate code, that you have to write before you can start using the library. However, it could be much simpler with the same effect:
import { createContextProvider, createContextHOC } from 'react-context-emission'
const GeoProvider = createContextProvider('geo', next => {
navigator.geolocation.watchPosition(next)
})
const withGeo = createContextHOC('geo')
Then you can start using this immediately, no need to write generic React.Component code every time
const App = withGeo(({geo}) => {
if(geo) {
return <dl>
<dt>latitude</dt>
<dd>{geo.coords.latitude}</dd>
<dt>longitude</dt>
<dd>{geo.coords.longitude}</dd>
</dl>
else {
return <p>Getting geoposition...</p>
}
})
ReactDOM.render(<GeoProvider><App /></GeoProvider>, container)
Thank you for the library! The idea is awesome, but I think that API could be better.
Each npm linked package has its own set of node_modules, therefore there might be multiple copies of react-broadcast
s required by an app (all with the same version). Each of them ofc maintains its own uid
counter but broadcasts
is a shared key on legacy context and all those copies override what the put in context.
Don't have better idea on how to deal with it than randomizing broadcasts
key (at least in development), thoughts? Can prepare a PR handling this if you find it worthwhile.
I'm opening this issue to discuss the change made in 57de4ff. I assume it was made so we can render <Link>
s in the router outside of the context of a <Router>
, right @ryanflorence?
It seems like the only place we should use the special string is for the key on context, to avoid clashes with others. Everything else we can make generic.
const [ Emitter, Subscriber ] = createContextEmission('keyOnContext')
<Emitter value={someValue}/>
<Subscriber>
{value => (
// ...
)}
</Subscriber>
Just seems like less docs to do it this way, no?
Thanks for this library.
I'm wanting to use Subscriber, but in a place where the context (broadcast) is not always set. In this case I would like to use either a default prop, or simply undefined.
Currently I can't do this, and I get this error:
browser.js:40 Uncaught Error: <Subscriber channel="filterStore"> must be rendered in the context of a <Broadcast channel="filterStore">
React 16 introduces the ability to return multiple children from a render
, so no need for this restriction any more.
Thanks :)
Is it possible to provide multiple values (i.e. localization and theme) on the same Provider (Broadcast) or should I wrap two providers/consumers inside a single component to achieve this?
@mjackson, @ryanflorence thanks for the lib!
I'm getting an error because in occasions I wrap certain components with <Subscriber>
when they are not descendants of <Broadcast>
. I know this was discussed (#6) and fixed but I'm still getting this error:
Uncaught Error: <Subscriber channel="concernX"> must be rendered in the context of a <Broadcast channel="concernX">
I should mention that I use different <Broadcast> - <Subscriber>
pairs for different concerns.
For example:
<Broadcast channel="concern1">
<Broadcast channel="concern2">
{whatever}
</Broadcast>
<Subscriber channel="concern2">
{whateverElse}
</Subscriber>
</Broadcast>
There's a problem in <Subscriber channel="concern2">
because this.context.broadcasts
exists but not for concern2
.
Is this the expected behaviour? Any workaround?
Is there a way to update the broadcast value from a separate file. Perhaps inside a method? Having a broadcast set with channel and value, I think I'll like something like:
// different class
import { Broadcast } from 'react-broadcast'
class Foo extend React.Component {
someMethod() {
Broadcast.update({name: 'Brendan Eich'})
}
}
Context API was finally stabilized in React 16.3 and is based on react-broadcast
.
Therefore, it's worth to mark this as deprecate or at least state clearly in docs that React Context API provide the same interface.
What do you think? Is there any reason not to deprecate 'react-broadcast'?
There is no possibility to work with two different components.
if possible then share the demo code.
I want to send data from one component to another component.
Is there a list of changes anywhere? Thanks!
โ๏ธ
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.