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.

Converter not returning the correct route

See original GitHub issue

I’m trying to integrate Junctions into a fresh code base where I’m already using React Router. I’m replicating the raw example and stripped out all the react-router components to try and get this to work. One tidbit to know is that all of my urls in the entire app need to be prefixed with /wp to get this to work alongside a legacy codebase.

My issue is that when I go to /wp/about or /wp/organizations I just see the word “Home”. I dug into it and saw that the route variable in AppScreen returns the same object at every one of those locations instead of the corresponding next junction that it should return.

Here’s my code:

import createBrowserHistory from 'history/createBrowserHistory'
import { createJunction, createConverter, locationsEqual } from 'junctions'

AppScreen.junction = createJunction({
  wp: {
    next: createJunction({
      about: {},
      help: {},
      organizations: {},
    }),
  },
});
function AppScreen({ route, locate, history }) {
  const junction = AppScreen.junction;
  let content;
  switch (route ? route.key : route) {
    case 'wp':
      content = <h2>Home</h2>;
      break;

    case 'about':
      content = <h2>About</h2>;
      break;

    case 'organizations':
      content = <h2>Organizations</h2>;
      break;

    case null:
      content = <h2>Home</h2>;
      break;

    // An undefined route indicates that the converter didn't know how
    // to handle the received location
    case undefined:
      content = <h2>Not Found</h2>;
      break;
  }
  return (
    <DefaultLayout
      content={content}
      history={history}
      locate={locate}
      route={route}
    />
  )
}

class JunctionsRouter extends Component {

  componentWillMount() {
    // As an application's Junction doesn't usually change at runtime,
    // we only ever need a single application-wide converter
    this.converter = createConverter(AppScreen.junction)

    // Handle the application's initial location
    this.handleLocationChange(this.props.history.location)
  }

  componentDidMount() {
    this.unlisten = this.props.history.listen(this.handleLocationChange.bind(this))
  }

  componentWillUnmount() {
    if (this.unlisten) {
      this.unlisten()
      this.unlisten = null
    }
  }

  handleLocationChange(location) {
    // Convert the Location object emitted by our history into a Route
    // through our application Junction
    const route = this.converter.route(location)
    // The Route produced by the converter may contain informatino which
    // the received Location object doesn't, due to default parameters
    // and default branches. If this is the case, create a new Location
    // object containing the new information, and redirect to it
    const canonicalLocation = route && this.converter.locate(route)
    if (route && !locationsEqual(location, canonicalLocation)) {
      this.props.history.replace(canonicalLocation)
    }

    // Add the route to component state to trigger a re-render
    this.setState({ route })
  }

  render() {
    return (
      // Screen Components always take `route` and `locate` props, so they
      // can decide what to render, and create Locations for any Links
      // and redirects.
      //
      // In this example, we also pass a history. Usually, this would be
      // passed via context, using a <Router> or <HistoryContext> component.
      <AppScreen
        route={this.state.route}
        locate={this.converter.locate}
        history={this.props.history}
      />
    )
  }

}

const history = createBrowserHistory();
export default render((
   <JunctionsRouter
      history={history}
      junction={AppScreen.junction}
      render={AppScreen}
    />
), document.getElementById('root'));

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
eriklharpercommented, Jan 19, 2017

The pathname: '/wp' param did the trick, thank you for that! After getting that /wp portion solved, I ran into the same exact problem right away though, where routes that I defined under next properties of top-level routes were not coming through in the route object.

But after looking at your “Base Location” example on this line I realized that I needed to explicitly pass the route.next junction as well as the locate function to my child components from my AppScreen component.

Once I figured that out, everything magically worked!

0reactions
eriklharpercommented, Jan 19, 2017

👍 That was it! Thanks! Good to see how these pieces fit together outside of using components that pass this all down with React context.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type Converter in Apache Camel is not working - Stack Overflow
I have a Camel Route which is calling another Camel endpoint using "direct-vm". At the end of the second Camel Route I am...
Read more >
'Float' converter in route not working #315 - pallets/flask - GitHub
In Flask 0.7.2, I have a route defined as: @app.route('/venues/ ... substitute int for float using the conversion notation, then the route works...
Read more >
Can't convert Linux error No route to host Network...
I try to convert Centos 5.5 buy I have error this at 3% FAILED: A general system error occurred: read No route to...
Read more >
Why is my custom parameter converter class not used?
I think the condition in applies() doesn't match with the route parameters, try this: public function applies($definition, $name, ...
Read more >
URL Routing — Werkzeug Documentation (2.2.x)
This converter is the default converter and accepts any string but only one path segment. Thus the string can not include a slash....
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