-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Children in <Link> cause Invariant Violation #143 #1605
Comments
Within my environment the error can be reproduced with something as simple as: // test.js
import Link from 'next/link'
export default props => (
<div>
<Link href='/'>
<a>example</a>
</Link>
</div>
) import Test from './test'
// render
// ...
<Test />
// ... Still production-only. |
Are you sure you don't have adjacent items in your link? <Link>
<div>...</div>
<img />
</Link> for instance, won't work and will give this error message |
@jonaswindey Thanks for the reply Jonas. Yes, it throws even with a single standard child. There must be something else going on. |
@flagello can we have a sample repo for that. |
Reopen when you add it. |
@arunoda , Hi, I have the same issue, this is my component. It's very simple. Only I need repeat a list with routes. Recently, install NextJS version 2.2.0 line: node_modules/next/dist/lib/link.js:174:35
|
I've seen that error before and restarting the server helped. @wzalazar: are you also only having the error in production mode? |
I was able to reproduce the error <Link prefetch href={{item.href}}><a>{item.name}</a></Link> works, but <Link prefetch href={{item.href}}> <a>{item.name}</a> </Link> doesn't and gives the error you mentioned. Notice the spaces before and after the @arunoda it seems like React considers the inner spaces as elements (or as null) which throws the error Error also occurs with only one space. |
It seems like this error is expected behaviour: https://discuss.reactjs.org/t/jsx-newline-bug/3242 Spaces are preserved in JSX so they are considered as child elements. |
@jonaswindey you're right, that is the reason of bug :) |
I have a scenario where this is pretty inconvenient -- in my blog I parse and render markdown with remark and remark-react, which tends to call I was able to work around in this case with: function MyLink({ children, href, as }) {
let validChildren = children;
if (Array.isArray(children) && children.length == 1) {
validChildren = children[0];
}
const props = {children: validChildren, href, as};
return <Link {...props} />;
} ... but it would be wonderful if this just worked :) I'm not sure exactly who's at fault in this case:
|
@joelburget I'm having the same issue (I think), but with js-lingui. I'm trying to create a simple example using next.js and js-lingui, but getting the So, who's to "blame"? Did you figure out anything @joelburget? |
@jonespen I didn't take this any further after I figured out my workaround. |
My problem got solved by putting a Text tag within the link and then adding the text inside the Text tag. Instead of, Try,
Hope this helps. |
Code
Error
React.Children.only expected to receive a single React element child.
Invariant Violation: Minified React error #143
Environment
Production-only,
v2.0.1
Add-on
fridays/next-routes
Workaround
Router.prefetch
The text was updated successfully, but these errors were encountered: