Unable to hit URL using GET method with params - reactjs

I have used react-router-dom with webpack and weppack-dev-server for development. My project is running, but when i directly hitting the url with paramameters in browser then it gives me
GET http://localhost:8080/archives/index.js net::ERR_ABORTED otherwise i hit the same url with Link component then it is running
//main.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, hashHistory} from 'react-router-dom';
import Archives from './modules/Archives';
import Layout from './modules/Layout';
import Featured from './modules/Featured';
import Settings from './modules/Settings';
import Home from './modules/Home';
console.log('hi');
ReactDOM.render((
<Router history={hashHistory}>
<div>
<Layout/>
<Route exact path={'/'} component={Home}/>
<Route path={'/featured'} component={Featured}/>
<Route path={'/archives/:id'} component={Archives}/>
<Route exact path={'/archives'} component={Archives}/>
<Route path={'/settings'} component={Settings}/>
</div>
</Router>
),document.getElementById('app'));
///////////////////////////////////////////////////////////////////////////
//Layout.js
import React from 'react';
import { Link } from 'react-router-dom';
import '../css/main.css';
const bstyle = {
color:'white',
background: 333333,
margin: '-9px',
};
export default class Layout extends React.Component{
render() {
return(
<div>
<h1>React Application</h1>
<ul>
<li><Link className='linktext' to='/'>Home</Link></li>
<li><Link className='linktext' to='/archives'>Archives</Link></li>
<li><Link className='linktext' to='/featured'>Featured</Link></li>
<li><Link className='linktext' to='/settings'>Settings</Link></li>
</ul>
{this.props.children}
</div>
)
}
}

Related

Reactjs: Components are not changing by clicking on NavLinks

Basically, I am learning React Routing with react-router-dom, version 5.0.0 and I have some components like Home, About, Contact etc. and I am trying to change each component by clicking on NavLink but not changing components just changing URL
Indexjs:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import './Style.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import Header from "./components/Header";
import Footer from "./components/Footer";
import Web from "./Web";
ReactDOM.render(
<React.Fragment>
<Header />
<Web />
<Footer />
</React.Fragment>
, document.getElementById('root'));
serviceWorker.register();
Web Componet:
import React, {Component} from 'react';
import {BrowserRouter as Router, Route, Link, NavLink, Redirect, Switch,withRouter} from "react-router-dom";
//import Route from 'react-router-dom/Route';
import {Home} from "./components/Home";
import {About} from "./components/About";
import {Contact} from "./components/Contact";
import {Products} from "./components/Products";
class Web extends Component {
render() {
return (
<Router>
<Switch>
<Route path={"/"} exact component={withRouter(Home)}/>
<Route path={"/products"} component={withRouter(Products)}/>
<Route path={"/about"} component={About}/>
<Route path={"/contact"} component={Contact}/>
<Route path={"/something"} render={
() => {
return (<h1>This is something page</h1>);
}
}/>
</Switch>
</Router>
);
}
}
export default Web;
Header Component:
import React, { Component } from 'react';
import logo from '../logo.jpg';
import {BrowserRouter as Router, Route, Link, NavLink, Redirect} from "react-router-dom";
class Header extends Component {
render() {
return(
<Router>
<header className={'header_area'}>
<div className="container">
<div className="logo">
<img src={logo} alt="logo"/>
</div>
<nav className={'main_nav'}>
<ul>
<li><NavLink to="/" activeStyle={{color:"green"}} exact>Home</NavLink></li>
<li><NavLink to="/products" activeStyle={{color:"green"}} exact>Products</NavLink></li>
<li><NavLink to="/about" activeStyle={{color:"green"}} exact>About</NavLink></li>
<li><NavLink to="/contact" activeStyle={{color:"green"}} exact>Contact</NavLink></li>
</ul>
</nav>
<div className="float_clear"></div>
</div>
</header>
</Router>
);
}
}
export default Header;
Home Component:
import React from 'react';
export class Home extends React.Component {
render() {
return (
<React.Fragment>
<article className={'article_area'}>
<div className="container article">
<h2>Home</h2>
</div>
</article>
</React.Fragment>
);
}
}
About Component:
import React from 'react';
export class About extends React.Component {
render() {
return (
<React.Fragment>
<article className={'article_area'}>
<div className="container article">
<h2>About us</h2>
</div>
</article>
</React.Fragment>
);
}
}
Can anyone help me?
You should use a single Router and all of your routes and links should have it as one of their parents.
Change your index:
<React.Fragment>
<Router>
<Header />
<Web />
<Footer />
</Router>
</React.Fragment>
and remove other <Router>s
Also you do not need to use withRouter for components inside Route. They will get react-router props directly.

React Router not rendering correctly

I had this working before, but I've made some changes in my app structure which has broken the routes. If I manually go to the route in the browser it works (the correct content loads). The href attribute is not rendering in the html.
I have a warning message in my console:
Warning: Failed prop type: Invalid prop to supplied to Link.
I don't nderstand why it's invalid.
What can I do to improve the code and fix the problem?
Index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import Header from './Header';
import Routes from './Routes';
import '../less/imports.less';
const App = () => (
<div>
<Header />
<main>
<Routes />
</main>
</div>
);
if (typeof window !== 'undefined') {
ReactDOM.render(
(
<App />
), document.getElementById('app'),
);
}
Routes.jsx:
import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import Dashboard from './Dashboard';
import About from './About';
import Blog from './Blog';
class Routes extends React.Component {
constructor(props) {
super(props);
this.state = {
active: 'active',
};
}
render() {
return (
<Router>
<div>
<nav>
<ul className="block-group">
<li className={this.state.active}><Link to={Dashboard}>Dashboard</Link></li>
<li><Link to={About}>About</Link></li>
<li><Link to={Blog}>Blog</Link></li>
</ul>
</nav>
<Route exact path="/" component={Dashboard} />
<Route path="/about" component={About} />
<Route path="/blog" component={Blog} />
</div>
</Router>
);
}
}
export default Routes;
I have just fixed this by changing the to href to the actual route strings. Not sure how it worked before.
<li className={this.state.active}><Link to="/">Dashboard</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/blog">Blog</Link></li>

Render Two Components at First Run

I'm working on a small react app with four components: Main, Nav, Timer, and Countdown. When I run it (open a new tab and load the app, not refresh), I want this app to render Main and Timer components simultaneously, but the app only renders Main Component and I see a blank page with only Nav component has been rendered.
I tried using IndexRoute but I got a strange error and it didn't work for me.
Here's my code:
import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter as Router, Route, IndexRoute, hashHistory} from 'react-router-dom';
import Main from 'Main';
import Timer from 'Timer';
import Countdown from 'Countdown';
ReactDOM.render(
<Router history={hashHistory}>
<div>
<Route path="/" component={Main} />
<Route exact path="/timer" component={Timer} />
<Route path="/countdown" component={Countdown} />
</div>
</Router>,
document.getElementById('app')
);
Main Component:
import React, {Component} from 'react';
import Nav from 'Nav';
class Main extends Component {
render() {
return (
<Nav />
);
}
}
export default Main;
Nav Component:
import React, {Component} from 'react';
import {NavLink} from 'react-router-dom';
class Nav extends Component {
render() {
return (
<div className="top-bar">
<div className="top-bar-left">
<ul className="menu">
<li className="menu-text">React Timer</li>
<li><NavLink to="/timer" activeClassName="active-link" activeStyle={{fontWeight: 'bold'}}>Timer</NavLink></li>
<li><NavLink to="/countdown" activeClassName="active-link" activeStyle={{fontWeight: 'bold'}}>Countdown</NavLink></li>
</ul>
</div>
<div className="top-bar-right">
<ul className="menu">
<li className="menu-text">Created by Milad Fattahi</li>
</ul>
</div>
</div>
);
}
}
export default Nav;
The other two components contain only a simple text element.
IndexRoute and hashHistory are not available in react-router-dom. Make use of Switch to render only first matching Route. You can then have the Child Routes in the component itself.
Also when you use Switch , have the route with path="/" at last otherwise it will match it at the begining and will not render your other routes
You can configure your time component to be a child of the Main component like
import {BrowserRouter as Router, Route} from 'react-router-dom';
ReactDOM.render(
<Router>
<Switch>
<Route path="/" component={Main} />
</Switch>
</Router>,
document.getElementById('app')
);
Then in Main component
import React, {Component} from 'react';
import Nav from 'Nav';
import {Route, Redirect} from 'react-router-dom'
class Main extends Component {
render() {
return (
<div>
<Nav />
<Redirect to="/timer" />
<Route exact path="/timer" component={Timer} />
<Route path="/countdown" component={Countdown} />
</div>
);
}
}
export default Main;

React Router Change transitionName on button click

I have added CSSTransitionsGroup to my React-Router to create page transitions. Now, I want to have different transitions, based on which link I click.
For example: I always want to use 'fade', except for when I click on the 'Work' button, then I want to use 'slideLeft'.
I am using React-Router 4, and the normal React Animation Add-Ons from https://facebook.github.io/react/docs/animation.html.
My App component with my Router and Transitions look like this:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Route, Switch } from 'react-router-dom';
import { Provider } from 'react-redux';
import CSSTransitionGroup from 'react-transition-group/CSSTransitionGroup' // ES6
import { ConnectedRouter } from 'react-router-redux';
import createHistory from 'history/createBrowserHistory';
const history = createHistory();
import Home from '../containers/Home';
import Work from '../containers/Work';
import BTS from '../containers/BTS';
import SinglePage from '../containers/SinglePage';
import NotFound from '../containers/NotFound';
import store from '../store';
export default class App extends Component {
render() {
return (
<Provider store={store}>
<ConnectedRouter history={history}>
<Route render={({ location }) => (
<CSSTransitionGroup
transitionName="fade"
transitionEnterTimeout={500}
transitionLeaveTimeout={300}>
<Switch key={location.key} location={location}>
<Route exact path="/" component={Home}/>
<Route exact path="/work" component={Work}/>
<Route exact path="/behind-the-scenes" component={BTS}/>
<Route path="/about" component={SinglePage}/>
<Route component={NotFound}/>
</Switch>
</CSSTransitionGroup>
)}/>
</ConnectedRouter>
</Provider>
);
}
}
And my Home component just have 3 simple buttons:
import React, { Component } from 'react';
import {connect} from 'react-redux';
import { Link, withRouter } from 'react-router-dom';
export default class Home extends Component{
render() {
return(
<div className="home page">
<main>
<ul>
<li><Link to="/work">Work</Link></li>
<li><Link to="/behind-the-scenes">behind the scenes</Link></li>
<li><Link to="/3eBtn">3e Btn</Link></li>
</ul>
</main>
</div>
)
}
}
So It always uses Fade, because I called transitionName="fade", but I want to call 'SlideLeft' when I click the '/work' Link.
Try this:
Put the Condition on value of transitionName, when the location will be work then assign the SlideLeft otherwise fade.
<CSSTransitionGroup
transitionName={location.pathName == "work" ? "SlideLeft" : "fade"}

Reactjs - Routing is not work on imported component

I'm new to ReactJS and i am developing single page app and I'm just trying to import my components to main component but while clicking on route link it showing blank state or null.
src\components\Detail.js
import React from 'react';
import ReactDOM from 'react-dom';
class Detail extends React.Component {
render() {
return (
<div className="App">
<div className="App-header">
<h2>Detail</h2>
</div>
<p>This is the Detail page.</p>;
</div>
);
}
}
export default Detail;
Above module trying to import in App.js for routing
App.js
import React from 'react'
import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom'
import { Detail } from './components/Detail.js'
const BasicExample = () => (
<Router>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/detail">Detail</Link></li>
<li><Link to="/topics">Topics</Link></li>
</ul>
<hr/>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
<Route path="/detail" component={Detail}/>
<Route path="/topics" component={Topics}/>
</div>
</Router>
)
I've not got any errors.
You need to import the other components too when you specify a route configuration and Detail is a default export so you should import without curly braces
import Home from './components/Home.js'
import About from './components/About.js'
import Detail from './components/Detail.js'
import Topics from './components/Topics.js'
Refer this answer for more info on named and Default export

Resources