Coder Social home page Coder Social logo

Issue on "/" paths about router HOT 14 CLOSED

tanstack avatar tanstack commented on May 22, 2024
Issue on "/" paths

from router.

Comments (14)

tannerlinsley avatar tannerlinsley commented on May 22, 2024

That's strange. / paths are forced to be exact all the time, and each Match should carry down its base via context.

from router.

tannerlinsley avatar tannerlinsley commented on May 22, 2024

from router.

diegodebonis avatar diegodebonis commented on May 22, 2024

The thing is that it's kinda solved when you render the component like this

<LocationProvider>
    <nav>
      <Link to="/">Home</Link>
      <Link to="dashboard">Dashboard</Link>
      <Link to="invoices">Invoices</Link>
    </nav>
    <div>
      <MatchFirst>
      <Match path="/">
        {console.log('AAA')}
        <div>This is Home</div>
      </Match>
      <Match path="dashboard">
        <div>This is the Dashboard</div>
      </Match>
      <Match path="invoices">
        <MatchFirst>
          <Match path="new">
            <Link to="..">Back</Link>
            <div>This is a new invoice!</div>
          </Match>
          <Match path=":invoiceID">
            {({ params }) => (
              <div>
                <Link to="..">Back</Link>
                This is invoice #{params.invoiceID}
              </div>
            )}
          </Match>
          <Match path="/">
            {({ params }) => {
              console.log('BBB')
              return(<div>
                This is invoice /
              </div>)
            }}
          </Match>
        </MatchFirst>
      </Match>
      </MatchFirst>
    </div>
  </LocationProvider>

That way it only console.log 'BBB' or 'AAA' accodingly the route.

from router.

tannerlinsley avatar tannerlinsley commented on May 22, 2024

Can you make a codesandbox that replicates this behavior for me? I'm having a hard time replicating it on my end.

from router.

diegodebonis avatar diegodebonis commented on May 22, 2024

Hi Tanner, here the example https://codesandbox.io/s/53jjln4654

You see that both console log executes, even when i enter to inovice. Only 1 console should execute.

Regards!

from router.

tannerlinsley avatar tannerlinsley commented on May 22, 2024

Nice. This is more clear. Thanks for the info. I'm still trying to figure out how or why this happens. Would be up to helping me investigate?

from router.

tannerlinsley avatar tannerlinsley commented on May 22, 2024

I feel like its some of this logic here: https://github.com/tannerlinsley/react-location/blob/master/src/index.js#L213-L220

from router.

niboc avatar niboc commented on May 22, 2024

Tanner, first of all I want to tell you that Diego and I are friends and we are talking about this topic since he discovered it.

My theory is that the child is rendered first, therefore the javascript code is executed, and then when rendering the parent, that child is hidden because it does not match the path, but the javascript has already been executed. On the other hand, when the child is a callback, this does not happen because the render trigger of the child occurs by the parent itself.

In conclusion, the routing works well, but the javascript of each Match child is executed regardless of whether match or no match

from router.

tannerlinsley avatar tannerlinsley commented on May 22, 2024

from router.

niboc avatar niboc commented on May 22, 2024

So, you're trying to say that the console.log should not be executed unless it meets the condition of match? I'm not sure how the interpreter works, but I get the feeling that it's running before the rendering.

  <Match path="/">
    {console.log("AAA")}
    <div>This is Home</div>
  </Match>
  <Match path="invoices">
    <MatchFirst>
      <Match path="/">
        {console.log("BBB")}
        <div>Invoices</div>
      </Match>
    </MatchFirst>
  </Match>
</MatchFirst>

When replacing the child by callback the problem disappears, which makes me think that it is not a match problem.

  <Match path="/">
    {() => {
      console.log("AAA")
      return <div>This is Home</div>
    }}   
  </Match>
  <Match path="invoices">
    <MatchFirst>
      <Match path="/">
        {()=>{
          console.log("BBB")
          return <div>Invoices</div>
        }}
      </Match>
    </MatchFirst>
  </Match>
</MatchFirst>

from router.

tannerlinsley avatar tannerlinsley commented on May 22, 2024

I think you're right as well. I was merely pointing out that it's not designed to render the content unless it matches. That definitely narrows the bug type we're looking for.

from router.

niboc avatar niboc commented on May 22, 2024

Yes, I did not mean to render, that word came out but I wanted to refer to the assembly of the component regardless of whether it is rendered or not. What I'm not 100% sure, is that the execution of those console.log is correct, it would be ideal not.

I hope you can find the explanation of what happens :)

from router.

niboc avatar niboc commented on May 22, 2024

Great Job Tanner, this version seems to work perfect! Also the basepath works well.

Thanks!

from router.

tannerlinsley avatar tannerlinsley commented on May 22, 2024

Thanks!

from router.

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.