-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preact and Relay #411
Comments
Hi Yasser - sorry I didn't reply to this earlier. Do you have an example that you started from or anything? I've not used Relay (though I'm extremely interested in it) so it's a little hard to reproduce. |
Hi Jason, Thank you for considering working on Relay ;) |
Awesome, I'll take a look when I get a little bit of spare time! |
I poked around at this a bit and it looks like Relay (here) is relying on It would seem that preact-compat would need to implement this in order to support using preact with Relay. I'm new to both Relay and the innards of React and have no clue how to fix this, but thought the leg work in tracking it down might be helpful. I'm also interested as it reduces a vendor bundle gzipped by almost 40% (148 -> 93k). |
Any progress on that? I think Relay compatibility would really push Preact forward, as it's a pretty common pattern to use React with Relay. :) |
Agreed, though I've not yet had time to fix this. If someone has a few spare cycles, it seems like we could just use a shim function that immediately invokes its first argument: function unstable_batchedUpdates(callback) {
callback();
} ... and just export it at the bottom of the library. |
Created a pull request preactjs/preact-compat#289, unfortunately it only helped with the error, nothing renders. Any ideas? |
@sjchmiela am running into this now, it looks like it may take a little more than just calling |
Oh boy. Not sure what I have gotten myself into 👍 |
hey @sjchmiela are you also using React Router in your project? Tracing through this, it looks like @developit was correct in suggesting that we simply need to call |
More importantly, @sjchmiela if you can check if "blank" means that |
Exactly, @nickhudkins, I use React Router and |
Cool then I'm close to at least finding out where it fails :) I'll let you
know.
…On Tue, Jan 31, 2017 at 3:07 PM Stanisław Chmiela ***@***.***> wrote:
Exactly, @nickhudkins <https://github.com/nickhudkins>, I use React
Router and undefined is being rendered to the DOM. :) Good job!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#411 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AASQdoyCDn3F2S6vSGNeuJU4RUIeShfqks5rX5SegaJpZM4K4tMl>
.
|
@twhid actually it's this function assigned to this property afaik (because dom, but they might be functionally similar) |
@sjchmiela good news it is not react-router :) nor is it Relay. I am able to successfully render a |
@developit
False alarm, sorry about that, though hoping your knowledge of the code may reveal what's going on here 👍 |
@sjchmiela you can follow along with my debugging here: https://github.com/nickhudkins/preact-relay-playground |
Quick investigation shows that it seems they're returning props.children, which might be an Array in some combination of preact + preact-compat. We'll need to guard against that in preact-compat somehow - there's already a place to do so. Just need to wrap render and strip arrays at the root down to their first value. |
@developit thanks for this :) :smashes head into wall: I've got some time later today so maybe I can get this wrapped up. |
@developit pardon my ignorance, but isn't that what is happening here. |
Yup, not sure what setup I was thinking would skip that. I did just publish [email protected], which addresses an issue where |
I just encountered this, it looks like I’m hoping someone understands this better than I do! :) |
Hit this same issue with mobx-react :(
|
mobx-react also breaks because of a missing @sjelfull try tagging your mobx-react to v4.3.0-rc1 or v.4.2.2. |
Anyone interested in submitting a PR to |
I've too have run into this same issue of relay components not rendering at all in Preact. After some investigation, I discovered is that some Relay components (at least in relay-modern) won't return a VNode when rendered, which seems to be why things aren't being put into the DOM. It appears that the render of a RelayFragmentContainer merely resolves the required data and returns the relevant Component, when then in turn is to be rendered to give a VNode. I've been able to hack in a fix for me that resolves that issue in the renderComponent function at preact@master:98 as follows: ...
if (!skip) {
rendered = component.render(props, state, context);
if (rendered && rendered.render) rendered = rendered.render() // << Added
// context to pass to the child, can be updated via (grand-)parent component
if (component.getChildContext) {
... So it sounds like React perhaps manages recursive rendering of some sort to produce a VNode, whereas Preact renders once and assumes it gets a VNode out of it. I might be way off here, and the issue is more than likely somewhere else, but I hope this helps! If I'm on the right track, happy to submit a PR - really want to get Relay and Preact playing nice; they're both such awesome tools. |
@adamdickinson is really possible to use Relay at preact? |
@renatobenks with the above patch, it runs very well. The only source of incompatibility seems to be a relatively minor difference in implementation between preact and react. I have noted a small potential issue with component recycling, but I haven't investigated it well enough to be able to reliably replicate. |
@adamdickinson Your suggestion worked for me! I haven't noticed the recycling issue yet. edit - it appears the additional The |
I'm confused what Relay is doing in the case where this fix is needed. There is a PFC that returns a component constructor - does react somehow know to instantiate that despite it not being attached to a VNode? I've never seen that behavior before... |
In React 16, it appears there's a check to determine the return type of a PFC in function mountIndeterminateComponent(
current,
workInProgress,
renderExpirationTime,
) {
...
value = fn(props, context);
...
if (
typeof value === 'object' &&
value !== null &&
typeof value.render === 'function' &&
value.$$typeof === undefined
) {
// Proceed under the assumption that this is a class instance
...
} else {
// Proceed under the assumption that this is a functional component
...
}
} This is within the context of Fibers though, making it harder to see why this is happening. I'll see how React 15 handles it |
In React 15, there's a check in mountComponent for a render method. If one isn't present, it's created. // Initialize the public class
var doConstruct = shouldConstruct(Component); // this returns false for functions
var inst = this._constructComponent(
doConstruct,
publicProps,
publicContext,
updateQueue,
);
var renderedElement;
// Support functional components
if (!doConstruct && (inst == null || inst.render == null)) {
renderedElement = inst;
...
inst = new StatelessComponent(Component);
this._compositeType = CompositeTypes.StatelessFunctional;
} else {
if (isPureComponent(Component)) {
this._compositeType = CompositeTypes.PureClass;
} else {
this._compositeType = CompositeTypes.ImpureClass; // Container is an ImpureClass
}
} I'm not sure if other libraries use this feature (a function returning a component) |
I may have worded my PR poorly, but Preact expects all PFCs to return VNodes, while React allows Components to be returned. |
Can someone say if something has changed since the release of Preact X? |
I just gave the todo example from https://github.com/relayjs/relay-examples a try but unfortunately relay relies on react internals and thus will never work with preact afaict. |
Looks like |
Hi,
I just tried to switch to preact on my relay project using preact-compat, it's compile but I have this error on the console.
I think preact is awesome, I reduced my gzip bundle from 160k to 124k with it, it's just to bad it's doesn't work. Is there something you can do about it?
Thanks
The text was updated successfully, but these errors were encountered: