loop through json reactJS - json

Id like to loop through a json file displaying the first level values as links as so:
<Route path='/acordes' component={Acordes}>
<IndexRoute component={La} />
<Route path='laSib' component={LaSib} />
<Route path='query' component={Query} />
</Route>
JSON
{
"la": {
"tonoMayor": "A",
"tonoMenor": "Am",
"septima": "A7",
"sexta": "A6",
"menorSexta": "Am6",
"menorSeptima": "Am7",
"septimaMayor": "A7M",
"disminuida": "Adim7",
"aumentados": "Aaug"
},
"la#Sib": {
"tonoMayor": "la#",
"tonoMenor": "A#m",
"septima": "A#m",
"sexta": "A#6",
"menorSexta": "A#m6",
"menorSeptima": "A#m7",
"septimaMayor": "A#7m",
"disminuida": "A#dim7",
"aumentados": "A#aug"
}
}
JS
import React, { Component } from 'react'
import { Router, Route, Link, IndexRoute, hashHistory, browserHistory, DefaultRoute, IndexLink } from 'react-router'
import Data from '!json!../json/acordes.json';
class App extends Component {
render () {
return (
<Router history={hashHistory}>
<Route path='/' component={Container}>
<IndexRoute component={Home} />
<Route path='/acordes' component={Acordes}>
<IndexRoute component={La} />
<Route path='laSib' component={LaSib} />
<Route path='query' component={Query} />
</Route>
<Route path='/about(/:name)' component={About} />
<Route path='/namedComponent' component={NamedComponents}>
<IndexRoute components={{ title: Title, subTitle: SubTitle }} />
</Route>
<Route path='*' component={NotFound} />
</Route>
</Router>
)
}
}
Then when the user clicks on {la} it should show the contents of it from the json.

Related

Transition between routes in react-router-dom v6.3

So I´m currently refactoring a website, and so I do with the rrd, which was on v5 in the previous website version.
Now, that the component doesn´t exist anymore we have to work with the new component as you probably know.
I previously used framer-motion to transition in and out between the routes like so:
<Switch location={location} key={location.pathname}>
<motion.div
initial="initial"
animate="in"
exit="out"
variants={pageVariants}
transition={pageTransition}>
<Route path="/audit" component={Audit} />
<Route exact path="/smartx" component={SmartX} />
<Route path="/smartx/erc20" component={TokenGenerator} />
<Route path="/createAudit" component={PaymentStatus} />
<Route path="/faq" component={FAQ} />
<Route path="/support" component={Support} />
<Route path="/terms" component={Terms} />
<Route path="/policy" component={Policy} />
<Route path="/about" component={About} />
<Route exact path="/">
<Redirect to="/audit" />
</Route>
</motion.div>
</Switch>;
Simply replacing the Switch component with the Routes component won´t work, since you can only have Route components as childs from .
Moving the <motion.div> one layer up over the Routes component leads to only one initial fade in transition on page load.
new (not quiet working version):
<AnimatePresence>
<PageLayout>
<motion.div
initial="initial"
animate="in"
variants={pageVariants}
transition={pageTransition}>
<Routes>
<Route path="/audit" element={<Audit />} />
<Route path="/smartx" element={<SmartX />} />
<Route path="/faq" element={<FAQ />} />
<Route path="/support" element={<Support />} />
<Route path="/terms" element={<Terms />} />
<Route path="/policy" element={<Policy />} />
<Route path="/about" element={<About />} />
</Routes>
</motion.div>
</PageLayout>
</AnimatePresence>;
Framer motion animations (equalin both old and new version):
const pageVariants = {
initial: {
opacity: 0
},
in: {
opacity: 1
},
out: {
opacity: 0
}
};
const pageTransition = {
type: 'tween',
ease: 'linear',
duration: 0.5
};
Any ideas on how to achieve a transition on each route switch?
Thanks in advance and cheers!
I think your solution is close to working. Move the PageLayout component and motion.div into a layout route that uses the current path as a React key.
Example:
import { Outlet } from 'react-router-dom';
const AnimationLayout = () => {
const { pathname } = useLocation();
return (
<PageLayout>
<motion.div
key={pathname}
initial="initial"
animate="in"
variants={pageVariants}
transition={pageTransition}
>
<Outlet />
</motion.div>
</PageLayout>
);
};
...
<Routes>
<Route element={<AnimationLayout />}>
<Route path="/audit" element={<Audit />} />
<Route path="/smartx" element={<SmartX />} />
<Route path="/faq" element={<FAQ />} />
<Route path="/support" element={<Support />} />
<Route path="/terms" element={<Terms />} />
<Route path="/policy" element={<Policy />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<Navigate to="/audit" replace />} />
</Route>
</Routes>

React Router 4 not rendering 404 on routes not found

I'm working on a React app that is using React Router 4. All of my routes are working great, however, if I go to a route that is not on the list I am not getting my 404 page. Any idea what I'm missing?
I'm using React 16 and React Router 4. This is for a sample app like Indeed.
export default function App() {
const companyRoutes = () => (
<Main>
<Route exact path="/companies/new" component={NewCompanyPage} />
<SubNav>
<PageBody companyPages>
<Switch>
<Route exact path="/companies" component={CompanyPage} />
<Route path="/companies/:companyId/edit" component={EditCompanyPage} />
<Route path="/companies/:companyId/jobs/:jobId/edit" component={EditJobPage} />
<Route path="/companies/:companyId/jobs/new" component={NewJobPage} />
<Route path="/companies/:companyId/jobs" component={CompanyJobsPage} />
<Route path="/companies/:companyId/locations" component={CompanyLocationsPage} />
<Route path="/companies/:companyId/recruiters" component={CompanyRecruitersPage} />
<Route path="/companies/:companyId/settings" component={CompanySettingsPage} />
<Route path="/companies/:companyId/applicants" component={CompanyApplicantsPage} />
</Switch>
</PageBody>
</SubNav>
</Main>
)
const jobRoutes = () => (
<Main>
<PageBody>
<Switch>
<Route exact path="/jobs" component={JobsPage} />
<Route path="/jobs/:jobId" component={JobPage} />
<Route path="/jobs/:jobId/apply" component={NewApplicationPage} />
</Switch>
</PageBody>
</Main>
)
const profileRoutes = () => (
<Main>
<SubNav>
<PageBody>
<Switch>
<Route exact path="/profiles" component={ProfilePage} />
<Route path="/profiles/:profileId/resume" component={ResumePage} />
<Route path="/profiles/:profileId/edit" component={EditProfilePage} />
<Route path="/profiles/:profileId/applications" component={ApplicationsPage} />
<Route path="/profiles/:profileId/settings" component={ProfileSettingsPage} />
</Switch>
</PageBody>
</SubNav>
</Main>
)
const loginRoutes = () => (
<LoginMain>
<Switch>
<Route exact path="/login" component={LoginPage} />
<Route exact path="/sign_up" component={LoginPage} />
</Switch>
</LoginMain>
)
const MainRoutes = () => (
<div>
<Route path="/companies" component={companyRoutes} />
<Route path="/jobs" component={jobRoutes} />
<Route path="/login" component={loginRoutes} />
<Route path="/profiles" component={profileRoutes} />
</div>
)
return (
<BrowserRouter>
<div>
<Route path="/" component={MainRoutes} />
<Route path="/404" component={NotFoundPage} />
</div>
</BrowserRouter>
)
}
[UPDATE]: Here you will find a proper, better tested example.
Not tested but try:
return (
<BrowserRouter>
<Switch>
<Route path="/" component={MainRoutes} />
<Route component={NotFoundPage} />
</Switch>
</BrowserRouter>
)
The idea is that inside a Switch, the router will try every route from the first one to the last one until it finds a corresponding path.
By having <Route component={NotFoundPage} /> at the very bottom of your routing, with no path specified, it will act as a wildcard, catching all unmatched urls.

React router : child routers not working

I have tried to define child routs with react router ( below code ) but child route is not working and also it doesn't returning any error message.
<Router>
<Route path="/" component={Main} >
<IndexRoute component={Dashboard} />
<Route path="create" component={UserList} />
<Route path="user/create" component={CreateUser} />
<Route path="createGroup" component={CreateGroup} />
<Route path="groups" component={GroupList} />
<Route path="premission" component={PremissionList}>
<Route path="create" component={CreatePremission} />
</Route>
</Route>
</Router>
I need to route premission/create to the create premission component.
To do multiple types of deep level/ nested routing you need to follow following approach:
<Router>
<Route path="/" component={Main} >
<IndexRoute component={Dashboard} />
<Route path="create" component={UserList} />
<Route path="user/create" component={CreateUser} />
<Route path="createGroup" component={CreateGroup} />
<Route path="groups" component={GroupList} />
</Route>
<Route path="/premission" component={PremissionList}>
<Route path="create" component={CreatePremission} />
</Route>
</Router>

Possible to mix JSX and regular object for routes in React Router?

With React Router, I see here that I can define my routes as either JSX or just a normal object. I have a use case where I would like to share settingsRoutes with another part of my application as a normal object but still use JSX for the other routes. The second code block is what I was like to do. Is this mixing of JSX and objects possible with react router??
const settingsRoutes = [{
path: 'settings',
component: Settings,
childRoutes: [
{
path: 'info',
component: SettingsInfo
}
]
}]
module.exports = settingsRoutes
var settingsRoutes = require('settingsRoutes')
<Route path='/' component={ Container }>
<Route path='register' component={ Register } />
<Route path='signin' component={ Signin } />
<Route path='signout' component={ Signout } } />
{ settingsRoutes }
</Route>
Do this:
<Route path='/' component={ Container }>
<Route path='register' component={ Register } />
<Route path='signin' component={ Signin } />
<Route path='signout' component={ Signout } } />
<Route childRoutes={settingsRoutes} />
</Route>

How to set react-router Route component with flux property.

function getFluxComponent(){
return <App flux={flux} />
}
render((
<Router history={history}>
<Route path="/" compopent={getFluxComponent()} >
<Route path="login" component={Login} />
<Route path="logout" component={Logout} />
<Route path="about" component={About} />
<Route path="watch" component={Watch} onEnter={requireAuth} />
</Route>
</Router>
), document.getElementById('example'))
I wanted to pass App component with flux property.
but I cannot set.
How to set flux property to App component.