Code Monkey home page Code Monkey logo

Comments (4)

jgoux avatar jgoux commented on June 2, 2024 1

Hey @exah, thanks for answering me!

I figured out that Portals won't solve it as well, as the context Provider has to be wrapping the whole component tree, and with react-router Routes component you end up with multiple nested contexts not necessarily wrapping both the Layout and the logically nested sections.

I wrote a similar solution as you suggested (just a little more complicated because react-router v6 isn't exposing the routing context, but maybe that's something I could ask!) :

// This Fill component will enable relative routing within the Layout
// as if we were at the logical layer from which the component was sent
const Fill = (
  props: React.ComponentProps<typeof DefaultFill> & {
    children: React.ReactElement;
  }
) => {
  // get the current pathname, like /projects/123/infos
  const { pathname } = useResolvedPath("");
  const { children, ...rest } = props;

  return (
    <DefaultFill {...rest}>
      {/* Define a new routing context with the current pathname as the basename */}
      <Routes basename={pathname}>
        {/*
           Render the component under an always matched route
           Now we can use <Link to="members"/> within the "children" and it will point to "/projects/123/infos/members"
         */}
        <Route path="*" element={children} />
      </Routes>
    </DefaultFill>
  );
};

from nano-slots.

jgoux avatar jgoux commented on June 2, 2024 1

FYI, I was able to properly forward the context by patching react-router so I can export their RouteContext : remix-run/react-router#7933 (comment)
I'll apply my patch until they expose the context officially 🤞.

from nano-slots.

exah avatar exah commented on June 2, 2024

Hey @jgoux,

This is really a question! Sadly react (at least to my knowledge) doesn't provide any way to get a context and move it somehow together with an element. Only rewriting the implementation to use ReactDOM.createPortal will work. I'm afraid in this case it will not be possible to use the library without react-dom and it might result in increasing the bundle size.

I will think about it, thanks :)

from nano-slots.

exah avatar exah commented on June 2, 2024

Right now, as a workaround, you can do something like this:

function MyContextAwareFill({ name, children }) {
  return (
    <MyContext.Consumer>
      {(contextValue) => (
        <Fill name={name}>
          <MyContext.Provider value={contextValue}>
            {children}
          </MyContext.Provider>
        </Fill>
      )}
    </MyContext.Consumer>
  )
}

(But only if the library exposes its context)

from nano-slots.

Related Issues (7)

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.