I am trying to add the live website URL i.e "https://covid19statswebsite.netlify.app/" so that when I click on my Button it should redirect me to the above URL. How can I do that?
Any Suggestions?
below is my Button,
import React from "react";
import "./Banner.css"
import BannerVideo from './videos/video-2.mp4'
import {Button} from "react-bootstrap";
import {useHistory} from "react-router-dom";
// import axios from './axios'
// import requests from "./Request";
function Banner(){
const history = useHistory()
return(
<div className="banner-container">
<video src={BannerVideo} autoPlay loop muted/>
<h1>ADVENTURE AWAITS</h1>
<p>What are you waiting for?</p>
/////////// Button That needs to redirect it ///////////////////////////////////////////////////////
<div className="banner-buttons">
<br/><br/><Button onClick={() => history.push("/covid")} className="covid"><span>COVID-19 Status</span></Button>
</div>
</div>
);
}
export default Banner
This is my App.js file which imports the Banner.js file above for Routes. I am not sure that the thing I want will happen with Routes or not. So I'd really love to know any other alternative if present.
import React, {useEffect} from 'react';
import './App.css';
import HomeScreen from "./screens/HomeScreen";
import LoginScreen from "./screens/LoginScreen";
import ProfileScreen from "./screens/ProfileScreen"
import {auth} from "./firebase";
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';
import {useDispatch, useSelector} from "react-redux";
import {login, logout, selectUser} from "./features/userSlice";
import Spain from "./countries/Spain";
import Covid from './Covid.js'
function App() {
const user = useSelector(selectUser)
const dispatch = useDispatch();
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged(userAuth => {
if (userAuth){
dispatch(login({
uid: userAuth.uid,
email: userAuth.email
}))
}else {
dispatch(logout());
}
});
return unsubscribe
}, [dispatch]);
return (
<div className="App">
<Router>
{!user ? (
<LoginScreen/>
):(
<Switch>
<Route path="/profile">
<ProfileScreen/>
</Route>
<Route exact path="/">
<HomeScreen/>
</Route>
<Route path="/spain">
<Spain/>
</Route>
//////////////////// Covid part //////////////////////////////////////////////////////////
<Route path="/covid">
<Covid/>
</Route>
</Switch>
)}
</Router>
</div>
);
}
export default App;
Below is the Covid.js which I created to check whether the Route works or not.
import React from "react";
function Covid(){
return(
<div className="covid">
<h1>hello</h1>
</div>
)
}
export default Covid
Edit:
import { Link } from "react-router-dom";
<Link
to={{
pathname:
"https://.......",}}
target="_blank">
<Button>...</Button>
</Link>
try to put the link in a hyperlink.
<a
href="https://covid19statswebsite.netlify.app/"
target="_blank">
<Button>...</Button>
</a>
Related
I have a menu with a "login" link to open to the login page. When I click on the "login" link it does nothing...doesn't open page, just empty click. My expectation was that the login page would be displayed. I am using an existing html template that uses UL lists for the menu. I'd like to keep using the UL list for the menu, if possible, as it fits in the existing CSS and layout of the page nicely.
I've researched react-router-dom, links, routes, etc for the last 3 hours...I can't find a solution for this, I see how others made it work somehow for their project, but when I try the solutions in my project it doesn't work for me...perhaps I've got something out of order? I'm new to react, but have read/watched a bunch of tutorials and would welcome any advice or guidance.
The problem appears to be inside "HeaderMenu.js". I've included the details of the components. thanks in advance.
The rendered anchor tag looks like this after React renders it:
Login
Here is how the project is set up:
Folder Structure
/src/Index.js
/src/App.js
/src/components/Home.js
/src/components/header.js
/src/components/HeaderMenu.js
/src/components/Login.js
Index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { BrowserRouter, Routes, Route } from "react-router-dom";
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
App.js
import './App.css';
import Home from "./components/Home";
function App() {
return (
<>
<Home />
</>
);
}
export default App;
Home.js
import Header from "./header";
import Body from './body';
import Footer from "./footer";
const Home = () => {
return (
<div className="page-container">
<Header />
<Body />
<Footer />
</div>
);
}
export default Home;
header.js
import React from 'react';
import HeaderMenu from "./HeaderMenu";
const Header = () => {
return <>
<header className="header-area header-sticky wow slideInDown" data-wow-duration="0.75s" data-wow-delay="0s">
<div className="container">
<div className="row">
<div className="col-12">
<nav className="main-nav">
<HeaderMenu />
</nav>
</div>
</div>
</div>
</header>
</>
}
HeaderMenu.js
import React from "react";
import { Routes, Route, Link } from "react-router-dom";
import Login from './Login';
const HeaderMenu = () => {
return (
<>
<ul className="nav">
<li className="scroll-to-section"><a href="#top" className="active" >Home</a></li>
<li className="scroll-to-section">About Us</li>
<li className="scroll-to-section">Services</li>
<li className=""><Link to={"/Login"}>Login</Link></li>
<li className="scroll-to-section"><div className="main-red-button">Contact Now</div></li>
</ul>
</>
)
}
export default HeaderMenu;
Login.js
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
const Login = () => {
return (
<div>
<h1> Login Page </h1>
</div>
);
};
export default Login;
You have to define routes. Like on which route, which component has to be render. For example: for "/login" you want to define a route in "app.js" file. Like below:
import "./App.css";
import Home from "./components/Home";
import { BrowserRouter, Routes, Route } from "react-router-dom";
// import all page components like following
// import Login from "./components/Login";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/login" element={<Login />} />
</Routes>
</BrowserRouter>
);
}
export default App;
I just started learning react router.
I am trying with a fairly basic example.
As I am trying with different routes, these routes are accessible when I try to hit them explicitly but when I am loading the page, its not loading the desired route. Can anyone please help me where I am making mistake.
import './App.css';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/js/bootstrap.js';
import Header from './components/Header';
import ProductCard from './components/ProductCard';
import ProductPage from './components/ProductPage';
import Cart from './components/Cart';
import { useSelector } from 'react-redux';
import LoginForm from './components/LoginForm';
import { useState, useEffect } from 'react';
import { Route, Switch, BrowserRouter } from 'react-router-dom';
function App() {
const showCartOption = useSelector(state => state.cart.showCartOption);
const isAuthenticated = useSelector(state => state.auth.isAuthenticated);
console.log("test - " + showCartOption + " " + isAuthenticated);
return (
<div class="m-3 shadow p-2">
<Header></Header>
<ProductPage />
<div class="row" >
<BrowserRouter>
{!isAuthenticated &&
<Route path="/login">
<LoginForm />
</Route>
}
{isAuthenticated && showCartOption &&
<ProductPage />
}
{isAuthenticated &&
<Route path='/products'>
<ProductPage />
</Route>
}
{isAuthenticated && !showCartOption &&
<Route path='/cart'>
<Cart />
</Route>
}
{!isAuthenticated && <LoginForm></LoginForm>}
</BrowserRouter>
</div>
</div >
);
}
export default App;
on loading the above snippet, console shows the value of flags as
showCartOption - true
isAuthenticated - false
I thought that it could be because it may go to every block until the end. I am not sure how this would work since I have added multiple conditions, so I also tried it with one single condition (added modified snippet below). In this case I was expecting that it would load the /login page. but that is also not happening. Its landing on the page where there is no path and hence no component.
2nd attempt :
import './App.css';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/js/bootstrap.js';
import Header from './components/Header';
import ProductCard from './components/ProductCard';
import ProductPage from './components/ProductPage';
import Cart from './components/Cart';
import { useSelector } from 'react-redux';
import LoginForm from './components/LoginForm';
import { useState, useEffect } from 'react';
import { Route, Switch, BrowserRouter } from 'react-router-dom';
function App() {
const showCartOption = useSelector(state => state.cart.showCartOption);
const isAuthenticated = useSelector(state => state.auth.isAuthenticated);
console.log("test - " + showCartOption + " " + isAuthenticated);
return (
<div class="m-3 shadow p-2">
<Header></Header>
<ProductPage />
<div class="row" >
<BrowserRouter>
{!isAuthenticated &&
<Route path="/login">
<LoginForm />
</Route>
}
</BrowserRouter>
</div>
</div >
);
}
export default App;
When you click on the button, the url changes but the redirect does not occur. If you reload manually, then it opens the desired page. Tell me how to fix this problem.
I suspect the problem is with router and history communication.
ROUTE.JS
import React, {Component} from "react";
import Cookies from "universal-cookie";
import history from "../../history";
const {SubMenu} = Menu;
export default class Sider extends Component {
outUser = () => {
const cookies = new Cookies();
cookies.remove("jwt", { path: '/' });
history.push("/");
}
render() {
return (
<div>
<Button onClick={this.outUser}>Выйти</Button>
</div>
);
}
}
APP.JS
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route
} from "react-router-dom";
import history from "./history";
import Auth from "./pages/auth/component"
import HomePage from "./pages/home/component";
import Cookies from 'universal-cookie';
const cookies = new Cookies();
function App() {
return (
<Router history={history}>
<Switch>
<Route exact component={Auth} path="/" />
<div className="App">
<div className="pages">
<Route component={HomePage} path="/home"/>
</div>
</div>
</Switch>
</Router>
);
}
I have a starter screen with two buttons which are login and register. Here is the code for the same:
import React from 'react';
import Header from './Header.jsx';
import Authentication from './AuthenticateCard.jsx';
import { Redirect } from 'react-router-dom';
function StarterScreen() {
function loginClick()
{
return(<Redirect to= "/login" />);
alert("Tru");
}
function registerClick()
{
console.log("Register");
}
return (
<div>
<Header header="My Water Chain"/>
<div className="WhiteCard">
<div className="whiteSpace">
</div>
<Authentication text="Login" onClickaction={loginClick} />
<Authentication text="Register" onClickaction={registerClick}/>
</div>
</div>
);
}
export default StarterScreen;
Here is my main app.js file:
import React from 'react';
import StarterScreen from './components/StarterScreen.jsx';
import LoginScreen from './components/LoginScreen.jsx';
import RegisterScreen from './components/RegisterScreen.jsx';
import 'bootstrap/dist/css/bootstrap.min.css';
import './App.css';
import {BrowserRouter,Route,Switch} from 'react-router-dom';
function App() {
function loginClick()
{
console.log("Login");
}
function registerClick()
{
console.log("Register");
}
return (
<div className="App">
<BrowserRouter>
<Switch>
<Route component={StarterScreen} exact path="/"></Route>
<Route component={LoginScreen} exact path="/register"></Route>
<Route component={StarterScreen} exact path="/login"></Route>
</Switch>
</BrowserRouter>
</div>
);
}
export default App;
when I click on login it just freezes and nothing happens afterwards. Screen does respond but no login action takes place with a bunch of errors:
Try:
import React from 'react';
import Header from './Header.jsx';
import Authentication from './AuthenticateCard.jsx';
import { withRouter } from 'react-router-dom';
function StarterScreen(props) {
function loginClick()
{
props.history.push('/login');
}
function registerClick()
{
console.log("Register");
}
return (
<div>
<Header header="My Water Chain"/>
<div className="WhiteCard">
<div className="whiteSpace">
</div>
<Authentication text="Login" onClickaction={loginClick} />
<Authentication text="Register" onClickaction={registerClick}/>
</div>
</div>
);
}
export default withRouter(StarterScreen);
Note here, I added withRouter to wrap your component and imported it from react-router-dom. Then I'm using props.history.push to navigate
You should not return a Redirect within event handler in StarterScreen, you can take advantage of the withRoutes high oder component or using useHistory hook to have access to the history of the navigator and change the route by calling push with the route where you want to navigate to.
import { withRouter } from 'react-router-dom';
const StarterScreen = ({history}) => {
const loginClick = () => {
history.push("/login");
}
return <div>
<Header header="My Water Chain"/>
<div className="WhiteCard">
<div className="whiteSpace">
</div>
<Authentication text="Login" onClickaction={loginClick} />
<Authentication text="Register" onClickaction={registerClick}/>
</div>
</div>;
}
export default withRouter(StarterScreen);
The withRouter high order component will pass 3 properties to you component has props match, location and history.
You can learn more here withRouter
Here is my way to redirect, i created a file call redirect.tsx, and then you can call from other file:
import { useHistory, withRouter } from "react-router-dom";
const Redirect = (props: any) => {
const to = (props.to || '/');
let history = useHistory();
history.push(to);
window.location.reload();
return (
<>
</>
)
}
export default withRouter(Redirect);
Without window.location.reload(); browser still redirect but it can not show anything!
Good day!
I try to go from one page to another in React and that works. Only I would also like to send data. I am new to React and have tried a few things but I cannot come up with the right solution.
As an example this is my index.js:
import React from "react";
import ReactDOM from "react-dom";
import { createBrowserHistory } from "history";
import { Router, Route, Switch, Redirect } from "react-router-dom";
import 'bootstrap/dist/css/bootstrap.min.css';
import Firebase from 'firebase';
import config from './firebase';
import choicePayment from 'views/choiceMenuPayment/choiceMenuPayment';
// core components
import Admin from "layouts/Admin.js";
import RTL from "layouts/RTL.js";
import "assets/css/material-dashboard-react.css?v=1.8.0";
const hist = createBrowserHistory();
ReactDOM.render(
<Router history={hist}>
<Switch>
<Route path="/admin" component={Admin} />
<Route exact path="/betalen/:test" component={choicePayment} />
<Redirect from="/" to="/admin/dashboard" />
</Switch>
</Router>,
document.getElementById("root")
);
This my internalRoutes.js:
import Dashboard from "#material-ui/icons/Dashboard";
import AddCircleOutlineIcon from '#material-ui/icons/AddCircleOutline';
import ExitToAppIcon from '#material-ui/icons/ExitToApp';
import CalendarToday from '#material-ui/icons/CalendarToday';
import EuroSymbol from '#material-ui/icons/EuroSymbol';
import Tune from '#material-ui/icons/Tune';
import AddIcon from '#material-ui/icons/Add';
// core components/views for Admin layout
import DashboardPage from "views/Dashboard/Dashboard.js";
import choicePayment from "views/choiceMenuPayment/choiceMenuPayment.js";
const internalRoutes = [
{
path: "/betalen",
name: "Keuze menu",
icon: Dashboard,
component: choicePayment,
layout: "/admin"
},
];
export default internalRoutes;
What I am trying to do is go from the ChaskDesk.js page to the choiceMenuPayment.js page when the link is clicked. For now that is possible with a variable "hello world", only this does not work, I would like that if the link is clicked in ChaskDesk.js the page choiceMenuPayment.js opens with the data "hello world" somewhere in the html as output. This is my current attempt, however, this does not work, is there anyone who can explain to me how I can make this?
The code on chaskdesk.js :
<Link to={{pathname: "/betalen",
state:{
name: "Hello Wolrd"
}}} component={internalRoutes} className="btn btn-primary">hello</Link>
the destination page:
import React, { useEffect } from "react";
import { useState } from 'react';
import { makeStyles } from "#material-ui/core/styles";
import styles from "assets/jss/material-dashboard-react/views/dashboardStyle.js";
import { Container } from "react-bootstrap";
import { Row } from "react-bootstrap";
import { Col } from "react-bootstrap";
import Close from '#material-ui/icons/Close';
import AddIcon from '#material-ui/icons/Add';
import RemoveIcon from '#material-ui/icons/Remove';
import { render } from "react-dom";
import { Button } from "react-bootstrap";
import { Link } from "react-router-dom";
import internalRoutes from "internalRoutes";
//const data = this.props.match.params.test;
export default function ChoicePayment() {
useEffect(()=> {
const {name} = this.props.location.state;
console.log(name);
})
return (
<div>
<Container >
<h3>Maak keuze</h3>
<Row>
<Col md={8} >
<form>
<Row>
<Col md={6}>
<label>
Klant:
</label>
<br/>
<Link to="/" className="btn btn-primary">hello</Link>
</Col>
</Row>
</form>
</Col>
</Row>
</Container>
</div>
);
}
Looks like you are merging class components (using this) with functional components.
The need to pass props into a functional component. Try changing ChoicePayment to something like this
export default function ChoicePayment(props) {
useEffect(()=> {
const {name} = props.location.state;
console.log(name);
})
...
You can always console log props outside of useEffect too
export default function ChoicePayment(props) {
console.log(props);
....
EDIT
This is working for me
const Admin = (props) => {
console.log(props);
return (
<div>
<div>Welcome to Admin</div>
<Link to={{ pathname: "/betalen", state: { name: "Hello Wolrd" } }} className="btn btn-primary">Test</Link>
</div>
);
}
function Test(props) {
console.log(props);
useEffect(() => {
const { name } = props.location.state;
console.log(name);
}, []);
return (
<div>
<div>Welcome to Test</div>
<Link to={{ pathname: "/admin" }} className="btn btn-primary">Admin</Link>
</div>
);
}
ReactDOM.render(
<Router history={hist}>
<Switch>
<Route path="/admin" component={Admin} />
<Route path="/betalen" component={Test} />
<Redirect from="/" to="/admin" />
</Switch>
</Router>,
document.getElementById("root")
);```