-
Notifications
You must be signed in to change notification settings - Fork 30
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
React.Children.only expected to receive a single React element child #19
Comments
Thank you, this is really helpful. I will work on allowing text nodes. |
Has this problem been solved? How can I solve this problem? Thank’s |
Adding a related example: This works:
This throws:
It would be nice to not have to wrap all components inside each Step |
The direct child of For now, to use a custom React component inside <Step>
<div>
<MyComponent />
</div>
</Step> I'm very open to hearing suggestions about how this can be resolved/improved. One solution could be that custom components could forward the ref to their DOM elements. React Scrollama could also check whether a child ref was a DOM element, and use |
(Note to self: See the wording on the error message |
@jsonkao OMG!! This was the error that I was having that confused me! Have to wrap the step-child (haha pun) in a I wouldn't be unopposed to having a |
You can use custom components but you have to handle ref forwarding. This is all not specifics to the lib but basics on how react works https://reactjs.org/docs/forwarding-refs.html#forwarding-refs-to-dom-components |
@maerzhase But isn't the issue that |
I am not sure what you mean by that. The step component is actually creating a ref for each step, thats why the child either needs to be a html element or a custom component that uses ref forwarding. If your "MyComponent" would use ref forwarding you wouldn't need to wrap a div around it. |
@inspectordanno I'm glad this helped! Sorry that the documentation is a bit scattered. @maerzhase I tried doing this once (I gave up) but I do vaguely remember either (a) needing to change |
Sorry to be dim. What should this look like with ref forwarding? I'm doing
But onSteoEnter doesn't fire. and I get this error
|
@gswirrl you are using standard HTML elements and therefore you don't need This works without
|
@maerzhase thanks, I tried to simplifiy what I'm trying to do and reduced it to the simplest case - maybe I didn't do a good job of explaining. What I'd tried to illustrate is that I need the
|
@gswirrl in your example you are dropping all the props that the const FancyStep = props => <Step {...props}>{/*some stuff here*/}</Step> Could you elaborate on "...that I need the |
@maerzhase I appreciate your kind reply. Yes I am aware the simplified pseudocode drops the props. I was trying to describe the use case in the simplest possible way. Obviously in practice it is a very great deal more complicated than the pseudo code I posted. If you nest step in another component it errors. In your example too. I'm building a higher level abstraction and in a nutshell the steps compose a good bit of additional functionality and get passed around in ways which are easy if they are wrapped in a component and awkward otherwise. My question though is really simple - is there a way to put a step inside another component which is the child of the scrolama component? |
@gswirrl yes this should work as long as you don't drop the props that are provided to the child components of the If you still experience problems please provide a reproducable example and no pseudo-code—otherwise I cannot help you with your problem. For the future we will definitely add more examples that will also include your use-case. |
Thanks! I hoped it would work like this too but it doesn't seem to. Adapting the readme example...
Which as I say, gives this error
which is why, in my very first post above, I was trying to forward the reference, it it seems that that too does not in fact work, as while it fixes this error, as I noted originally, it fails again downstream of there. I hope I'm just doing this wrong, but at the moment i ant find a way at all to have the Step in a nested component 🙁 many many thanks again for your help! |
@gswirrl aaaaand in your example code your are still dropping the props. Sorry I didn't know anymore how to explain it to you—So I set up a codesandbox that illustrates what you are trying to achieve: https://codesandbox.io/s/react-scrollama-custom-step-component-rnr7d?file=/src/App.js |
@maerzhase thank you I'm most grateful for your patience and help. Youve actually nailed it here, but a bit differently than we both thought.... The sandbox works excellently, as you say, but when I ran that same code verbatim in my env, it didn't work. Turns out I am on react-scrollama": "2.2.16" and if I change the sandbox to that it errors too in the same way mine was doing. I think that might be the issue? 😀 However.... that new 2.3 version introduces a new problem. I'm in next.js and ssr is dying because |
@gswirrl hmm you are right, i must admit i joined the project rewriting the code-base which is now on the prerelease About SSR: Scrollama highly depends on |
@maerzhase man you're a star - the sandbox completely solved the mystery thank you. Appreciate the next tips too - yes I shall do something like that - it's clearly GOT to be a client side thing 😀 once again tons of thanks for going to that extra bit of trouble. I'm really grateful. |
@gswirrl hej man no problem! I really enjoy contributing in the open source community and its cool if this library supports you in building something great! if you need any support in anything else just hit us up! |
If you try to recreate the example in the README, you get this error:
The problem is that
<Step>
is taking text nodes in the example:However, if you replace the text nodes any element, then everything works correctly:
React.Children.only(children)
throws with a text node: https://github.com/jsonkao/react-scrollama/blob/master/src/Step.js#L25I don't know if you want to allow text nodes, or update the README to wrap the text in elements.
The text was updated successfully, but these errors were encountered: