score:1
Accepted answer
After finding a typo in my original code ('/maps/' where the path should have been '/map/') I found another 'gotcha' which is this:
If the route expects a URL parameter, and it is not supplied, then the route doesn't seem to render at all. My route is defined as:
```JSX
<Route path="/map/:_id" render={({ match }) => (
<Map
params={match.params}
/>
)} />
If you try to navigate to 'http://localhost:3000/map/' then the component doesn't render. If you put any value on the end e.g. 'http://localhost:3000/map/dummyvalue' it renders.
I've now got a tidier version working:
```JSX
import React, { Component } from 'react';
import { withRouter, Switch, Route, Link } from 'react-router-dom';
import { Meteor } from 'meteor/meteor';
import { withTracker } from 'meteor/react-meteor-data';
// Mongodb collection
import { Maps } from '../../api/maps/maps.js';
// User Accounts
import AccountsUIWrapper from '../AccountsUIWrapper.jsx';
import Home from '../pages/home';
import Map from '../pages/map';
import About from '../pages/about';
function NewMap(props) {
function handleClick(e) {
e.preventDefault();
let history = props.history;
Meteor.call('newMap', {'name': 'new map'}, (error, result) => {
history.push(`/map/${result}`);
});
}
let disabled = 'disabled';
if (Meteor.userId()) { disabled = '';}
return (
<button disabled={disabled} onClick={handleClick}>New Map</button>
);
}
class App extends Component {
renderNewMap() {
const history = this.props.history;
return (
<NewMap history={history}
/>
);
}
render() {
let newMap = this.renderNewMap();
return (
<div className="primary-layout">
<header>
<AccountsUIWrapper />
{newMap}
<nav>
<ul>
<li><Link to='/'>Home</Link></li>
<li><Link to='/about'>About</Link></li>
</ul>
</nav>
</header>
<main>
<Switch>
<Route exact path='/' component={Home}/>
<Route path="/map/:_id" render={({ match }) => (
<Map
params={match.params}
/>
)} />
<Route path='/about' component={About}/>
</Switch>
</main>
</div>
);
}
}
export default withRouter(withTracker(() => {
const mapsHandle = Meteor.subscribe('maps');
return {
'maps': Maps.find({}).fetch(),
'loading': !mapsHandle.ready(),
'currentUser': Meteor.user(),
};
})(App));
Source: stackoverflow.com
Related Query
- How to click a button and navigate to a new route with URL params using React Router 4
- How to concatenate URL and string, and get data from API at click a button using axios in REACT JS
- How can I find and click a button that has no text using React Testing Library?
- How to add a tab dynamically when we click a button using React js and Bootsrap 4
- How to Create a dropdown Button with see more option and on click toggle should not happen in react js?
- React router - click on card and redirect to a new page with content of that card (card details) using useParams and reacthooks
- on click of a button how can I re-direct to the outlook mail with automatic body and sender using Reactjs?
- How to get a login button to navigate to a new page once clicked. I have downloaded and used the React router dom
- With React Router v4, how to navigate to a path based upon the existing browser location but with new params interpolated?
- How to create a new page and route programatically in React.js with React Router
- Im using React Router and I want one of my route links with path="/" to be the home page on reload.. How can you do that?
- Route to new component with props on button click React
- How to stop playing loaded video and load a new one when url changes using React Player?
- Navigate to a new page with React when I click on a button
- How to add a new textfield on button click and add integer value of that input field to an array in react
- How to get params from url with React front end and express backend?
- react router v4 about how to match two url with one route and one component
- How to create an input formfield with button that uploads a file using react and javascript?
- How to map through array of objects and create a new array of objects with a new added property using react and javascript?
- how to write one GET api call using axios instance with and without params in React JS
- Trying to create a multi-functional button using react js {on first click - saves data, turns "view", and on second click - navigate to saved page}
- How to navigate on path by button click in react router v4?
- How to mock history.push with the new React Router Hooks using Jest
- How do I store JWT and send them with every request using react
- How to display 404 when URL doesn't match any route and when the URL contains /#/ using ReactJS
- How to test snapshots with Jest and new React lazy 16.6 API
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- How to create a preview (Image and description) to the Url when shared link on Social media,Gmail and Skype using React JS?
- Using different API url for development and production with React and axios
- How to nest Scene and navigate with direction='vertical' in React Native Router Flux?
More Query from same tag
- Using Materialize CSS and MaterialUI in the same React Application
- Isolate specific config files from main bundle
- Adding objects fetched from local restify server into object in state
- declare variable to multiple state values
- BabelPluginRelay: Expected plugin context to include "types", but got:[object Object] when upgrading from 1.1.0 to 1.4.1. Why?
- Splitting Styled-Components into multiple files but export them as one file
- Set state based on an a property present in array of objects : React+Typescript
- React: How to render same page that includes multiple components and forms for 2 types of user with different permissions?
- Emitting and handling global events with react
- Pod install error after upgrading to React Native 0.63.0
- update nested state object with array inside
- how can I choose to send pass different props to a react component according to the current viewport size?
- How to make exe file from Reactjs and a nodejs app?
- Updating multiple array object values with useState in React
- How to manage title, meta tags, etc, inside React with server rendering + streaming?
- How to type initial value in React Context of object data fetched from API
- React Bootstrap Checkbox in Nav -> NavItem does not rerender on click
- Object doesn't support property or method 'readFile' (node: fs)
- Access data from arr of objects using props passed to children - error
- React - How to get the button element I clicked?
- Event not firing on a newly created window with Reactjs
- How to solve import error for created component in ReactJs
- Making a table in react js without proper key values in json data
- split string within react if statement
- React MaterialUI LinearProgress with gradient
- Switch case return multiple component
- Delete tag in uncontrolled "ChipInput" using react hook form
- What is this css structure called?
- ReactJS: How to collapse Material UI TreeView after expanding?
- Custom back navigation for reactnavigation back button