Coder Social home page Coder Social logo

Why can't my code use context? about solid HOT 9 CLOSED

thep0y avatar thep0y commented on September 22, 2024
Why can't my code use context?

from solid.

Comments (9)

deluksic avatar deluksic commented on September 22, 2024 1

On the other hand, depending on what you need, this seems like overkill. You should be able to get away with applying :first-child and :last-child in CSS with no js needed.

from solid.

deluksic avatar deluksic commented on September 22, 2024

Two things here.

  1. Running props.children in component body "reads" the children, i.e. instantiates them outside of the context, and then you only put the child inside the context after it was initialized.
  2. Use children helper in order for your component to stay reactive and instantiate children only once. (reading children once in component body will break reactivity). Remember props.children will recreate children every time it is accessed.

from solid.

thep0y avatar thep0y commented on September 22, 2024

On the other hand, depending on what you need, this seems like overkill. You should be able to get away with applying :first-child and :last-child in CSS with no js needed.

The Input component only needs to add some classes when it is a child component of Compact, and cannot be styled solely through first-child and last-child.

from solid.

thep0y avatar thep0y commented on September 22, 2024

Two things here.

  1. Running props.children in component body "reads" the children, i.e. instantiates them outside of the context, and then you only put the child inside the context after it was initialized.
  2. Use children helper in order for your component to stay reactive and instantiate children only once. (reading children once in component body will break reactivity). Remember props.children will recreate children every time it is accessed.

Using the children helper has no effect on useContext. The context received in Input is always undefined. The code above is just for illustration.

Using the same approach, it is possible to pass and receive context in React.

from solid.

deluksic avatar deluksic commented on September 22, 2024

Sorry I overlooked that the context is inside the for loop. I believe what you're trying to do is not possible because accessing children instantiates all of them, so there is no opportunity to inject a different context for each child.

I don't have the full context of your application code, but I would strongly recommend not going for this pattern even if React supports it. You will be generating a different context each with two accessor functions for each child only to style first and last element differently.

Could you perhaps add the compact class if it is a child of compact, and then in css use .compact:first-child?

from solid.

thep0y avatar thep0y commented on September 22, 2024

In Compact, child components need to have unique styles by adding their respective -compact classes, such as input-compact and button-compact. These styles should not be implemented in compact.scss but rather in input.scss or button.scss, as -compact may use some unique variables. For example, input-compact needs to use var(--input-boder-radius), and input-compact:first-child needs to use var(--input-border-radius-sm).

from solid.

deluksic avatar deluksic commented on September 22, 2024

That should be relatively easy:

  1. Compact renders a <CompactContext.Provider value={true}> around its children (default is false)
  2. Children const isCompact = useConext(CompactContext) and render their respective .input-compact or .button-compact class (if isCompact) which you can also additionally style for .input-compact:first-child etc.

from solid.

thep0y avatar thep0y commented on September 22, 2024

The class I want to add is not only first-child and last-child, but also the size and shape of Input and Button when they are children of Compact, which are also passed down from the context of Compact.

from solid.

ryansolid avatar ryansolid commented on September 22, 2024

This is a good discussion. I'm going to move the issue there. It is behaving as expected. There is no bug here but I'm interested to see how a solution forms out. We do have this sort of chicken and the egg issue since you need to create the elements to iterate over them so there is no way to get context in to them before creating them.

from solid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.