Skip to content
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} function and menu #4004

Closed
giannis-dallas opened this issue Feb 13, 2018 · 6 comments
Closed

{children} function and menu #4004

giannis-dallas opened this issue Feb 13, 2018 · 6 comments
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@giannis-dallas
Copy link

giannis-dallas commented Feb 13, 2018

Hello,

This is a more general question. I am trying to figure out this code from the tutorial.

import React from "react";
import Link from "gatsby-link";

const ListLink = props =>
  <li style={{ display: `inline-block`, marginRight: `1rem` }}>
    <Link to={props.to}>
      {props.children}
    </Link>
  </li>

export default ({ children }) =>
  <div style={{ margin: `0 auto`, maxWidth: 650, padding: `1.25rem 1rem` }}>
    <header style={{ marginBottom: `1.5rem` }}>
      <Link to="/" style={{ textShadow: `none`, backgroundImage: `none` }}>
        <h3 style={{ display: `inline` }}>MySweetSite</h3>
      </Link>
      <ul style={{ listStyle: `none`, float: `right` }}>
        <ListLink to="/">Home</ListLink>
        <ListLink to="/about/">About</ListLink>
        <ListLink to="/contact/">Contact</ListLink>
      </ul>
    </header>
    {children()}
  </div>

Is {children()} a native function to gatsby?
How is the {props.children} passed to ListLink function?
Is there a specific tutorial to read about it?

Thank you and sorry if I am abusing the forum for a general question

Greetings,
Giannis

@pieh
Copy link
Contributor

pieh commented Feb 13, 2018

Usually props.children is general react way (not gatsby specific) of accessing content between opening and closing tags:

<ListLink to="/">Home</ListLink>

props.children here will be string 'Home' - if you nest other components there, then props.children will be those components. You can check more for example here - https://learn.co/lessons/react-this-props-children

props.children() function that is passed to layout is gatsby specific construct on the other hand (and this is only used in layouts). That is used to render page component (with some wrappers around it to make gatsby navigation work). Normally children isn't a function, so I can see why this might be confusing (maybe worth renaming it something else to avoid confusion), but unfortunately this have to be function to make it work for gatsby.

@giannis-dallas
Copy link
Author

Thanks for clarifying the confusion @pieh

@fk fk added type: question or discussion Issue discussing or asking a question about Gatsby type: documentation An issue or pull request for improving or updating Gatsby's documentation labels Feb 13, 2018
@fk
Copy link
Contributor

fk commented Feb 13, 2018

@pieh @KyleAMathews I'm wondering if https://www.gatsbyjs.org/docs/building-with-components/#layout-components would be a good spot to document this?

I did a quick search for props.children on gatsbyjs.org and (weirdly enough) only found https://www.gatsbyjs.org/docs/migrating-from-v0-to-v1/#_templatejs-is-now-srclayoutsindexjs. Maybe mentioning the PR comment referenced there along @pieh's explanation from this issue already helps?

In regards to only finding the v0 to v1 migration article when searching for props.children I'm wondering if highlighted code blocks are indexed by algolia at all? /cc @m-allanson

@m-allanson
Copy link
Contributor

@fk I think only <li> and <p> tags are indexed. We could update the config to include <code> blocks but it might make the results quite noisy? Looking through the configs at https://github.com/algolia/docsearch-configs it seems like most sites don't index <code> blocks.

@fk
Copy link
Contributor

fk commented Feb 13, 2018

We could update the config to include <code> blocks but it might make the results quite noisy

Good point 🤦‍♂️ 🙁
Thank you for looking into how other people handle things!

@heyalbert
Copy link

To be honest, I don't understand {children()} in layout.js either. What does this exactly do? How can I manipulate it? What I have to keep in mind? Can I use it in other files, too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

5 participants