Change webpage in node js button click - node.js

I am currently working with node.js and have this question,
How can i go from one page to the other by clicking the "test" button? I am trying to get from ./Landing to ./Login by pressing the button
This is my code:
import React, { Component } from 'react'
import { Route, NavLink, HashRouter } from "react-router-dom";
import Login from "./Login";
class Landing extends Component {
render () {
return (
<button type="submit" class="btn btn-test">test</button>
);
}
}
export default Landing
Thank you in advance for your help!

J, you can look for the react-router-dom
On a summarised way, you will need to this:
Import react-router-dom dependencies
Create router.js file, like this:
import { Switch, Route, BrowserRouter } from 'react-router-dom'
import { components } from './YOUR_COMPONENTS_FOLDER'
const Router = () => (
<main>
<BrowserRouter>
<Switch>
<Route exact path="/" component={components.mainComponent} />
<Route exact path="/login" component={components.loginComponent} />
<Route component={components.notFoundComponent} />
</Switch>
</BrowserRouter>
</main>
)
export default Router
Add this router.js file into your App.jsx:
import Router from './router'
...
class component extends React.PureComponent {
...
render() {
...
return (
...
<Router />
...
)
}
}
Once you add Router into App.jsx file, remove any reference of your old code for main page.
To open a page, you will add this to your component:
import { Link } from 'react-router-dom'
...
render () {
<Link to="/login">
YOUR BUTTON or something else to be clicked
</Link>
}
...
Here are some references that may help you:
https://reacttraining.com/react-router/web/guides/quick-start
https://blog.pshrmn.com/entry/simple-react-router-v4-tutorial/

Hence you can use react router you could add one more route
Look at below example link for more details
[CodePen] https://codepen.io/dwarka/pen/PLEMWX

Related

What is wrong with my pagination in my react app? (with semantic ui)

So I am making a react app with semantic ui, that communicates with my own backend and displays news. I want to use pagination, that I tried to make it like it is said in this article: https://www.codementor.io/#maseh87/paginating-a-react-app-with-semantic-ui-x1g4a0t79
So here is my app.tsx:
import { Component } from "react";
import { Navigate, Route, Routes } from "react-router-dom";
import Navbar from "./components/navbar/Navbar";
import MainPage from "./pages/MainPage";
import NewsDetailPage from "./pages/NewsDetailPage";
interface AppProps {}
interface AppState {}
class App extends Component<AppProps, AppState> {
render() {
return (
<div>
<header>
<Navbar />
</header>
<div className="ui container" style={{paddingBottom: "2rem"}}>
<Routes>
<Route path="/news/:id" element={<NewsDetailPage />} />
<Route path="/news" element={<MainPage />} />
<Route path="*" element={<Navigate to="/news" replace />} />
</Routes>
</div>
</div>
);
}
}
export default App;
And my main page, this includes the pagination:
import { useEffect, useState } from "react";
import NewsList from "../components/news list/NewList";
import { NewsModel } from "../models/news.model";
import { newsService } from "../services/news.service";
import MainNews from "../components/main news/MainNews";
import { Pagination } from 'semantic-ui-react';
const MainPage = () => {
const [news, setNews] = useState<NewsModel[]>([]);
const [activePage, setActivePage] = useState(1);
const [apiUrl, setApiUrl] = useState('http://localhost:3001/api/');
useEffect(() => {
const fetchNews = async () => {
setNews(await newsService.getNews());
};
fetchNews();
}, []);
const [main, ...rest] = news;
const onChange = (pageInfo: any) => {
setActivePage(pageInfo.activePage);
setApiUrl('http://localhost:3001/api/?page=' + activePage.toString());
};
return (
<div>
<MainNews main={main} />
<NewsList news={rest} />
<Pagination
activePage={activePage}
onPageChange={onChange}
totalPages={10}
ellipsisItem={null}
/>
</div>
);
};
export default MainPage;
So I want to display the first piece of news with a different style and then the list of remaining news. But I also want this on only the first page, on the rest I just want to see the list.
But what is wrong with the pagination things? It displays the numbers of the pages, but nothing happens when I click on the next page.

Firebase deployment only showing background gray color, no errors I suspect it has somthing to do with MUITheme

UPDATE!!!
Ok I have fixed all issues, they just needed (foo) to be added to them and for MyButton.js it just needed to be a variable and then exported, HOWEVER I still have this gray background when i create it as a static server, or try to serve it on firebase?! this is what I see.
[![enter image description here][5]][5]
I suspect it is either express or MUITheme from material-ui its also worth noting that if I click the deployed firebase link it opens automatically in my chrome that has react developer tools and redux devtools connected to the site and runs perfectly fine. Below is my App.js code.
import React, { Component } from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import './App.css';
import { ThemeProvider } from '#material-ui/core/styles';
import { unstable_createMuiStrictModeTheme as createMuiTheme } from '#material-ui/core/styles';
import themeFile from './util/theme';
import jwtDecode from 'jwt-decode';
// Redux
import { Provider } from 'react-redux';
import store from './redux/store';
import { SET_AUTHENTICATED } from './redux/types';
import { logoutUser, getUserData } from './redux/actions/userActions';
// Components
import Navbar from './components/layout/Navbar';
import AuthRoute from './util/AuthRoute';
// import themeObject from './util/theme';
// Pages
import home from './pages/home';
import login from './pages/login';
import signup from './pages/signup';
import user from './pages/user';
import axios from 'axios';
const theme = createMuiTheme(themeFile);
axios.defaults.baseURL = "https://NOT SHOWING YOU THIS BUT TRUST ME ITS CORRECT.cloudfunctions.net/api";
const token = localStorage.FBIdToken;
if(token){
const decodedToken = jwtDecode(token);
if(decodedToken.exp * 1000 < Date.now()){
store.dispatch(logoutUser())
window.location.href = '/login';
} else {
store.dispatch({ type: SET_AUTHENTICATED });
axios.defaults.headers.common['Authorization'] = token;
store.dispatch(getUserData());
}
}
class App extends Component {
render() {
return (
<ThemeProvider theme={theme}>
<Provider store={store}>
<Router>
<Navbar />
<div className="container">
<Switch>
<Route exact path="/" component={home}/>
<AuthRoute exact path="/login" component={login} />
<AuthRoute exact path="/signup" component={signup} />
<Route exact path="/users/:handle" component={user} />
<Route exact path="/users/:handle/scream/:screamId" component={user} />
</Switch>
</div>
</Router>
</Provider>
</ThemeProvider>
)
}
}
export default App;

React, Redux and Router authentication

Don't know if this is a good question or not. However, I have been searching for hours and I cannot find a satisfactory answer for my problem. To keep things short, in my router, when I route to /dashboard; I want to check if the user is logged in. If the user is not logged in, I want to redirect him to my login page. Only problem, I am receiving my isAuthenticated(is logged in) variable from redux, which takes a second to load. Thus, whenever I check, my variable turns out to be null.
Here is the code,
import React, { Component } from "react";
import Home from "./pages/Home";
import Testimonials from "./pages/Testimonials";
import Pricing from "./pages/Pricing";
import Login from "./pages/Login";
import Register from "./pages/Register";
import DashboardHome from "./pages/Dashboard/DashboardHome";
import {
BrowserRouter as Router,
Switch,
Route,
Redirect
} from "react-router-dom";
import { connect } from "react-redux";
import PropTypes from "prop-types";
class AppRouter extends Component {
static propTypes = {
isAuthenticated: PropTypes.bool
};
render() {
return (
<Router>
<Switch>
<Route path="/register">
<Register />
</Route>
<Route path="/login">
<Login />
</Route>
<Route path="/pricing">
<Pricing />
</Route>
<Route path="/testimonials">
<Testimonials />
</Route>
<Route path="/dashboard">
this.props.isAuthenticated ?<DashboardHome /> :
<Redirect to="/login" />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</Router>
);
}
}
const mapStateToProps = state => ({
isAuthenticated: state.auth.isAuthenticated
});
export default connect(mapStateToProps, {})(AppRouter);
Much appreciation to anyone who helps out. Still new to react and redux.
You can set your components defaultProps. More info here: https://reactjs.org/docs/react-component.html#defaultprops
AppRouter.defaultProps = {
isAuthenticated: false
};
Note: React's experimental Suspense component could be useful to you if you're looking to render a loading component while the user is being authenticated. More info here: https://reactjs.org/docs/concurrent-mode-suspense.html

Getting warning while adding noLayout prop to custom routes list in react-admin

How to remove following warning or any alternative way to add custom routes in react-admin without default appLayout?
Warning: You should not use <Route component> and <Route render> in
the same route; <Route render> will be ignored
import React, { Component } from 'react';
import { Admin, Resource } from 'react-admin';
import { Route } from 'react-router-dom';
import restProvider from 'ra-data-simple-rest';
class App extends Component {
render() {
return (
<Admin
title="react-admin"
dataProvider={restProvider('http://localhost:3000')}
customRoutes={[<Route exact path="/custom" component={()=><div>Custom page without layout</div>} noLayout /> ]}
>
<Resource name="test" list={() => <div>Test Resource</div>} />
</Admin>
);
}
}
export default App;
Use children instead of component in your route:
class App extends Component {
render() {
return (
<Admin
title="react-admin"
dataProvider={restProvider('http://localhost:3000')}
customRoutes={[<Route exact path="/custom" noLayout><div>Custom page without layout</div></Route>]}
>
<Resource name="test" list={() => <div>Test Resource</div>} />
</Admin>
);
}
}
export default App;

React rendering multiple components despite using 'exact'

I have a React app with a conflict between two routes:
<Route exact path="/app/participants/register" component={ParticipantRegistration}/>
<Route exact path="/app/participants/:participantID" component={ParticipantDetailed}/>
The first Route, renders fine. However, due to the /:participantID wildcard in the path of the second Route - both the ParticipantRegistration and ParticipantDetailed components render - despite using the exact parameter.
How can I get React to render only the ParticipantRegistration component when the path is /app/participants/register and not render the ParticipantDetailed component underneath?
I would prefer not to have to modify the paths as the app has a few other conflicts like this and keeping track of all the different paths is difficult enough as it is.
You can use a Switch to render only the one route at a time.
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import "./styles.css";
function App() {
return (
<BrowserRouter>
<Switch>
<Route path="/x/register" component={() => <p>x</p>} />
<Route path="/x/:id" component={() => <p>y</p>} />
</Switch>
</BrowserRouter>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
You can play with the code here

Resources