question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to define context of sub-components using parent component's config

See original GitHub issue

@frctl/fractal version: 1.1.3 + @frctl/nunjucks version: 1.0.3

I love Fractal so far, but I’m struggling a bit with including sub-components via method B (define context data in the parent componet’s config), documented here http://fractal.build/guide/components/sub-components#b-define-all-context-data-in-the-parent-component-s-config.

I’m using Nunjucks as my template engine, and .js files for component configs. Recreating the doc-example, I’m not able to pass in a different text to my button using the parent component’s config.

{# button.njk #}
<button type="button" name="submit">{{ text }}</button>
// button.config.js
module.exports = {
    title: 'Button',
    context: {
        text: 'A Button'
    }
};
{# search-box.njk #}
<div class="searchbox">
    <label for="search">{{ label }}</label>
    <input type="search" name="keywords" id="search">
    {% render '@button' %}
</div>
// search-box.config.js
module.exports = {
    title: 'Search Box',
    context: {
        label: 'Search here',
        button: {
            text: 'Go!'
        }
    }
};

According to the documentation, I could expect the following result:

<!-- button.html -->
<button type="button" name="submit">A Button</button>
<!-- search-box.html -->
<div class="searchbox">
    <label for="search">Search here</label>
    <input type="search" name="keywords" id="search">
    <button type="button" name="submit">Go!</button>
</div>

But instead I get the text “A Button” (defined in button.config.js) in both cases:

<button type="button" name="submit">A Button</button>
<!-- search-box.html -->
<div class="searchbox">
    <label for="search">Search here</label>
    <input type="search" name="keywords" id="search">
    <button type="button" name="submit">A Button</button>
</div>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mikkelromcommented, Aug 24, 2018

Hi @lucasctd. It’s actually better to include your CSS in the head of the preview layout: https://fractal.build/guide/components/preview-layouts

Hope this helps! 🙂

1reaction
LeBenLeBencommented, Aug 24, 2018

@lucasctd If you use Nunjucks, see 7iomka comment, if you use Handlebars:

{{render '@example' partialData}}

Or to merge with component default context:

{{render '@example' partialData merge=true}}

See Using Handlebars section on https://fractal.build/guide/core-concepts/views

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update React Context from inside a child component?
Here I'm setting the defaults for language ('en') and a setLanguage function which will be sent by the context provider to the consumer(s)....
Read more >
How To Share State Across React Components with Context
In this tutorial, you'll share state across multiple components using React context. React context is an interface for sharing information ...
Read more >
How and When to Use Component Context in Svelte
Data added via setContext is only visible to the current component and its children; there is no way to alter the context of...
Read more >
Breaking down CI/CD complexity with parent-child and multi ...
With parent -child pipelines we could break the configurations down into two separate tracks by having two separate jobs trigger child ...
Read more >
Compound Components In React - Smashing Magazine
This tutorial explains how this can be achieved with the help of using the Context API and React to build components by using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found