react router changes navigation but does not render page using redux - node.js

I have got a simple react-router-redux application going on where /home has a button which when clicked should navigate to /profile page. Currently my code looks like this.
actions/index.js
import { push } from 'react-router-redux'
import * as actionTypes from '../constants'
const homeClicked = () => {
return { type: actionTypes.HOME_CLICK }
}
const profileClicked = () => {
return { type: actionTypes.PROFILE_CLICK }
}
export const handleHomeClick = () => {
return (dispatch) => {
dispatch(homeClicked())
dispatch(push('/profile'))
}
}
export const handleProfileClick = () => {
return (dispatch) => {
dispatch(profileClicked())
dispatch(push('/'))
}
}
containers/HomeContainer.js
import React from 'react'
import { connect } from 'react-redux'
import * as actions from '../actions'
import { withRouter } from 'react-router-dom'
import PropTypes from 'prop-types'
class Home extends React.Component {
handleClick = () => {
this.props.handleHomeClick();
}
render() {
return (
<div className='Home'>
<button onClick={this.handleClick}>Home</button>
</div>
)
}
}
Home.propTypes = {
handleHomeClick: PropTypes.func.isRequired
}
const mapStateToProps = () => {
return {}
}
export default withRouter(connect(mapStateToProps, actions)(Home))
containers/ProfileContainer.js
import React from 'react'
import { connect } from 'react-redux'
import * as actions from '../actions'
import { withRouter } from 'react-router-dom'
import PropTypes from 'prop-types'
class Profile extends React.Component {
handleClick = () => {
this.props.handleProfileClick();
}
render() {
return (
<div className='Profile'>
<button onClick={this.handleClick}>Profile</button>
</div>
)
}
}
Profile.propTypes = {
handleProfileClick: PropTypes.func.isRequired
}
const mapStateToProps = () => {
return {}
}
export default withRouter(connect(mapStateToProps, actions)(Profile))
reducers/index.js
import { HOME_CLICK, PROFILE_CLICK } from '../constants'
import { combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux'
const clickReducer = (state={ message: 'HOME' }, action) => {
switch(action.type) {
case HOME_CLICK:
return { message: 'PROFILE' };
case PROFILE_CLICK:
return { message: 'HOME' };
default:
return state
}
}
export default combineReducers({
clicking: clickReducer,
routing: routerReducer
})
constants.js
export const HOME_CLICK = 'HOME_CLICK'
export const PROFILE_CLICK = 'PROFILE_CLICK'
history.js
import { createBrowserHistory } from 'history'
export default createBrowserHistory()
index.js
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux';
import createRoutes from './routes'
import rootReducer from './reducers'
import thunk from 'redux-thunk'
import browserHistory from './history'
import reduxLogger from 'redux-logger'
import { createStore, applyMiddleware } from 'redux'
import { syncHistoryWithStore, routerMiddleware } from 'react-router-redux';
const middlewares = applyMiddleware(
thunk,
routerMiddleware(browserHistory),
reduxLogger
);
const store = createStore(rootReducer, middlewares)
const history = syncHistoryWithStore(browserHistory, store)
const routes = createRoutes(history)
ReactDOM.render(
<Provider store={store}>
{routes}
</Provider>,
document.getElementById('root')
)
routes.js
import React from 'react'
import { Router, Route, Switch } from 'react-router'
import HomeContainer from './containers/HomeContainer'
import ProfileContainer from './containers/ProfileContainer'
const createRoutes = (history) => {
return (
<Router history={history}>
<Switch>
<Route exact path='/' component={HomeContainer}/>
<Route path='/profile' component={ProfileContainer}/>
</Switch>
</Router>
)
}
export default createRoutes
app.js
import express from 'express'
import config from './config'
import path from 'path'
const app = express()
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')))
app.get('*', (req, resp) => {
resp.render('index');
})
app.listen(config.port, config.host, () => {
console.info('Server listening to', config.serverUrl())
})
This code is changing the url but not rendering the profile page when the home button on the home page is clicked. Also here's a link of the picture of redux logger output.
I am stuck on this for a few hours and other SO answers have not been much of a help. Any help would be appreciated.

When you click home it should render the home route as you have it written now? Isn't that what it is supposed to do.

Related

useContext auth hook returning null

I have created a useAuth hook in order to allow the user access to the private route once authorised the authorising cookie is stored properly but the useAuth hook seems to be returning null. I also tried printing the value of setUser but it doesn't seem to print after the line setUser(res.data.currentUser) but the one before is printed I cannot figure out why.
The cookies are stored and set properly it just seems to be the auth hook that return setUser as null even though the line console.log("RES>DATA = ", res.data.currentUser); logs the correct details of the cookie but when I try to manually change the url to the private route after the cookie is stored it returns a null for the setUser returned from the auth hook and so access to private route isn't granted. This is done using react for frontend with node js and express for the backend. If any more context is necessary please let me know.
useAuth hook:
import { useState, useContext, useEffect } from "react";
import { UserContext } from "./UserContext";
import axios from "axios";
export default function useAuth() {
const { setUser } = useContext(UserContext);
const [error, setError] = useState(null);
useEffect(() => {
console.log("USE effect");
setUserContext();
});
//set user in context and push them home
const setUserContext = async () => {
console.log("In here");
return await axios
.get("auth/checkAuth")
.then((res) => {
console.log("RES>DATA = ", res.data.currentUser); // PRINTS THE CORRECT COOKIE VALUES
setUser(res.data.currentUser); // THIS SEEMS TO BE NULL?????
console.log("SET USER + ", setUser); // THIS DOES NOT PRINT
})
.catch((err) => {
setError(err);
});
};
return {
setUser,
};
}
Private route:
import React, { useContext } from 'react';
import {Route, Redirect, Navigate} from 'react-router-dom';
import { UserContext } from '../hooks/UserContext';
import useAuth from '../hooks/useAuth';
export default function PrivateRoute({children}) {
const auth = useAuth();
console.log("aUTH in priv = ", auth);
return auth ? children : <Navigate to="/"/>
}
App.js:
import "./App.css";
import { Redirect, Route, Routes, Switch } from "react-router-dom";
import useFetch from "./useFetch";
import { useEffect, useState, useRef } from "react";
import axios from "axios";
import Activities from "./components/Activities";
import HomePage from "./components/Home";
import Map from "./components/Map";
import checkUser from "./hooks/checkUser";
import { UserContext } from "./hooks/UserContext";
import useFindUser from "./hooks/checkUser";
import PrivateRoute from "./components/PrivateRoute";
function App() {
const [auth, setAuth] = useState(false);
const [activities, setActivities] = useState([]);
const notInitialRender = useRef(false);
const { user, setUser, isLoading } = useFindUser(); // works as expected
return (
<div className="App">
<UserContext.Provider value={{ user, setUser, isLoading }}>
<Routes>
<Route path="/" element={<HomePage />}></Route>
<Route path="/Activities" element={<Activities />} />
<Route
path="/Private"
element={
<PrivateRoute>
<Map />
</PrivateRoute>
}
/>
</Routes>
</UserContext.Provider>
</div>
);
}
export default App;
auth/checkAuth express route:
export const checkAuth = (req, res) => {
let currentUser;
console.log("res.cookies = ", req);
if (req.cookies.currentUser) {
// res.send(200);
currentUser = req.cookies.currentUser; // set to user object in cookies
console.log("current user = ", currentUser);
// return 200 status code
} else {
currentUser = null;
}
//res.json(currentUser);
res.status(200).send({ currentUser });
};
Map component which is private route component:
import react from 'react';
function Map() {
<div>
<p style={{color:'red'}}>You have access to the private route</p>
</div>
}
export default Map;
UserContext file:
import { createContext } from "react";
export const UserContext = createContext("null");
I tried logging values and using useEffect to call the function when the useAuth hook is called but couldn't figure it out

Authentication reactjs website with Firebase using email and password

I tried to create an authentication website with Firebase using email and password. I can't even load the Login page.
Here's Auth.js
import React, { useState, useEffect} from "react";
import { auth } from './config'
import { onAuthStateChanged } from "firebase/auth";
export const AuthContext = React.createContext();
export const AuthProvider = ({ children }) => {
const [currentUser, setCurrentUser] = useState(null);
useEffect(() => {
onAuthStateChanged(auth, (user) => {
setCurrentUser(user);
})
}, [])
return (
<AuthContext.Provider value={{currentUser}}>
{children}
</AuthContext.Provider>
)
}
And this is Login.js
import React, {useContext ,useState } from "react";
import Form from "react-bootstrap/Form";
import Button from "react-bootstrap/Button";
import "./Login.css";
import { auth } from './config'
import { signInWithEmailAndPassword } from "firebase/auth";
import { AuthContext } from "./Auth";
import { useHistory } from "react-router-dom";
const Login = () => {
let history = useHistory();
const handleSubmit = (event) => {
event.preventDefault();
const { email, password } = event.target.elements;
signInWithEmailAndPassword(auth, email.value, password.value)
.then((userCredential) => {
const user = userCredential.user;
console.log(user.uid);
})
.catch((error) => {
console.log(error.massage);
});
}
const currentUser = useContext(AuthContext);
if(currentUser) {
return history.push('/dashboard');
}
return (
<div className="Login">
<h1>Login</h1>
<Form onSubmit={handleSubmit}>
//Login Form
</Form>
</div>
);
}
export default Login
And DashBoard.js
import React, {useContext} from 'react'
import { AuthContext } from './Auth'
import { auth } from './config'
import { signOut } from 'firebase/auth'
import { useHistory } from "react-router-dom";
const DashBoard = () => {
const currentUser = useContext(AuthContext);
let history = useHistory();
if(!currentUser) {
return history.push('/login');
}
const signOutFunc = () => {
signOut(auth)
}
return (
<div>
<div className='container mt-5'>
<h1>Welcome</h1>
<h2>If you see this you are logged in.</h2>
<button className='btn btn-danger' onClick={signOutFunc}>Sign Out</button>
</div>
</div>
)
}
export default DashBoard;
Lastly App.js
import { BrowserRouter as Router, Route, Switch} from 'react-router-dom'
import Login from './Login'
import DashBoard from './DashBoard';
import { AuthProvider } from './Auth'
function App() {
return (
<AuthProvider>
<Router>
<Switch>
<Route exact path="/login" component={Login} />
<Route exact path="/dashboard" component={Dashboard} />
</Switch>
</Router>
</AuthProvider>
);
}
export default App;
When I open /login, it would send me to /dasgboard immediately. If I typed /login again it gives me this error
Error: Login(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
I can't figure it out what's wrong with it. Please help me.
Thank you
You have multiple places in your code where you return history.push('/dashboard'); or another path. You should return there a null:
if(!currentUser) {
history.push('/login');
return null
}

running test using mount creates: Target container is not a DOM element.

the app runs fine but when I run test i.e) 'yarn run test', it shows error: Target container is not a DOM element. I am not importing the file where the reactdom render happens. I'd appreciate your help solving this issue. thanks.
Steps to Reproduce
please look at reproducible demo
Expected Behavior
It runs fine when I run the app using 'yarn start'. Problem only occurs when I run test.
I expected the test to run correctly.
Actual Behavior
Error comes up, when I 'mount' the app.js component.
Invariant Violation: Target container is not a DOM element.
Reproducible Demo
try running 'yarn run test' in the repo, or look at code samples below. thanks
https://github.com/SeunghunSunmoonLee/react-graphql-d3-vx
```
// containers/app/index.test.js
import App from './index.js'
import { Provider } from 'react-redux'
import { push, ConnectedRouter } from 'react-router-redux';
import { ApolloProvider } from 'react-apollo';
import { shallow, mount } from 'enzyme';
import store from '../../store.js'
// import configureStore from '../../../configureStore';
// import createHistory from 'history/createBrowserHistory';
// import { GET_POSTS } from './index.js';
// const initialState = {};
const history = createHistory({ basename: '/' });
// const store = configureStore(initialState, history);
export const client = new ApolloClient({
uri: 'https://fakerql.com/graphql',
});
const asyncFlush = () => new Promise(resolve => setTimeout(resolve, 1000));
// const mocks = [
// {
// request: {
// query: GET_POSTS,
// },
// result: mockData,
// },
// ];
describe('<Histogram/> in Home Page with real data from server', async () => {
let screen;
beforeEach(async () => {
screen = mount(
<ApolloProvider client={client}>
<Provider store={store}>
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</Provider>
</ApolloProvider>
)
screen.find(Provider).prop('store').dispatch(push('/'));
await asyncFlush();
})
it('shuld have postsByMonthSorted in component state', () => {
expect(screen.state().postsByMonthSorted).not.toHaveLength(0);
expect(screen.state().postsByMonthSorted).toHaveLength(12);
})
it('should render svg properly', () => {
expect(screen.find('svg').to.have.lengthOf(4))
})
it('it should be defined', () => {
expect(Histogram).toBeDefined();
});
// it('it should have the .vx-bar class', () => {
// expect(
// HistogramWrapper({
// className: 'test'
// }).prop('className')
// ).toBe('vx-bar test');
// });
})
// containers/app/index.js
import React from 'react'
import { Route, Link } from 'react-router-dom'
import Home from '../home'
import About from '../about'
// <header style={{width: '400px', height: '50px', display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
// <Link to="/">Home</Link>
// <Link to="/about-us">About</Link>
// </header>
class App extends React.Component {
render() {
return (
<div>
<main>
<Route exact path="/" component={Home} />
<Route exact path="/about-us" component={About} />
</main>
</div>
)
}
}
export default App
// src/index.js
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'connected-react-router'
import { ApolloProvider } from 'react-apollo';
// import { ApolloClient } from 'apollo-client';
// import { HttpLink } from 'apollo-link-http';
// import { InMemoryCache } from 'apollo-cache-inmemory';
import ApolloClient from 'apollo-boost';
import store, { history } from './store'
import App from './containers/app'
import 'sanitize.css/sanitize.css'
import './index.css'
export const client = new ApolloClient({
uri: 'https://fakerql.com/graphql',
});
// const gql_URL = 'http://localhost:4000';
//
// const httpLink = new HttpLink({
// uri: gql_URL,
// });
// const cache = new InMemoryCache();
// const client = new ApolloClient({
// link: httpLink,
// cache,
// });
render(
<ApolloProvider client={client}>
<Provider store={store}>
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</Provider>
</ApolloProvider>,
document.getElementById('root')
)
```
I was importing something from index.js inside app.js , which after removal, solved the issue.
I had a similar error. I solved it by changing describe to it.

Browser history needs a DOM error when rendering react-router v4 and redux on the server

I'm trying to server-render content for my app that uses react-router v4,redux, and express but I get Browser history needs a DOM error in the terminal. I also use react-router-config to keep my routes more organized. Saw a solution that suggested that one should create the store on the server, so I tried copypasting the code from store.js file to the server, however it didn't work out. What can I do to fix this extremely unpleasant error?
routes.js
const routes = (
{
component: App,
routes: [
{
path: '/',
exact:true,
component: Home
},
{
path: '/login',
exact:true,
component: Login
},
{
path: '/registration',
exact:true,
component: Registration
},
{
path: '/person/:id',
exact:true,
component: UserPage
},
{
path: '/myPage',
exact:true,
component: MyPage
},
{
path: '/goodBye',
exact:true,
component: GoodBye
},
{
path: '*',
component: NoMatch
}
]
}
);
App.js
import React from 'react';
import ReactDOM from 'react-dom';
//import '../styles/app.scss'
import {Provider} from 'react-redux';
import {Grid} from 'react-bootstrap';
import store from './store/store';
import routes from './routes';
import { createBrowserHistory } from 'history';
import { syncHistoryWithStore } from 'react-router-redux';
import { renderRoutes } from 'react-router-config';
import { BrowserRouter as Router} from 'react-router-dom';
import { ConnectedRouter, Switch } from 'connected-react-router';
class App extends React.Component {
render() {
return (
<Provider store={store}>
<Grid fluid={true}>
<ConnectedRouter history={createBrowserHistory()}>
<Switch>
{renderRoutes(routes)}
</Switch>
</ConnectedRouter>
</Grid>
</Provider>
);
}
}
const isBrowser = typeof window !== 'undefined';
if(isBrowser) {
ReactDOM.render(<App/>, document.getElementById('root'));
}
Route handler:
import express from 'express';
import React, {Component} from 'react';
import { renderToString } from 'react-dom/server';
import routes from '../../js/routes';
import {StaticRouter} from 'react-router';
import { renderRoutes } from 'react-router-config';
import { Provider } from 'react-redux';
import store from '../../js/store/store';
const router = express.Router();
router.get('*',(req,res) => {
let context = {};
const content = renderToString(
<Provider store={store}>
<StaticRouter location={req.url} context={context}>
{renderRoutes(routes)}
</StaticRouter>
</Provider>
);
if(context.status === 404) {
res.status(404);
}
res.render('index', {title: 'Express', data: store.getState(), content });
});

React-Router can't import components within components

I am having a problem understanding how routes and pages should be structured using react-router, I learn the principles of react and am working off of a post placed on 24ways currently. The issue I am facing is the ability to import components.
For example I have my router setup as such:
// Router
export const routes = {
path: '',
component: appComponent,
childRoutes: [
{
path: '/',
components: {nav: navbarComponent, content: indexComponent}
},
{
path: '/join',
component: {nav: navbarComponent, content: joinComponent}
}
]
};
// appComponent
import React from 'react';
export default class appComponent extends React.Component {
render() {
const { nav, content } = this.props;
return (
<div>
<div className="nav">
{nav}
</div>
<div className="content">
{content}
</div>
</div>
);
}
}
Is there a way that I don't have to do this an instead am able to directly import into each of my components, for example this instead:
// Router
export const routes = {
path: '',
component: appComponent,
childRoutes: [
{
path: '/',
components: indexComponent
},
{
path: '/join',
component: joinComponent
}
]
};
// appComponent
import React from 'react';
import Navbar from 'Navbar';
export default class appComponent extends React.Component {
render() {
return (
<div>
<Navbar />
{this.props.children}
</div>
);
}
}
Have been searching all over and can't find a solution to this problem, would like to use react-router and react but it currently does not seem feasible if this is not possible. From my understanding of react the ability to build and reuse components within one another was possible.
Here is the navbar component:
import React from 'react';
import Navbar from 'react-bootstrap/lib/Navbar';
import Nav from 'react-bootstrap/lib/Nav';
import NavItem from 'react-bootstrap/lib/NavItem';
export default class navbarComponent extends React.Component {
render () {
return (
<Navbar inverse>
<Navbar.Header>
<Navbar.Brand>
React-Bootstrap
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse className="bs-navbar-collapse">
<Nav>
<NavItem eventKey={1} href="/">Home</NavItem>
</Nav>
<Nav pullRight>
<NavItem eventKey={1} href="/join">Sign Up</NavItem>
<NavItem eventKey={2} href="/login">Login</NavItem>
</Nav>
</Navbar.Collapse>
</Navbar>
)
}
}
The code for my server is as follows:
// module imports
import express from 'express';
import http from 'http';
// react imports
import React from 'react';
import { renderToString } from 'react-dom/server';
import { match, RoutingContext } from 'react-router';
// route imports
import { routes } from './lib/routes';
const app = express();
app.use(express.static('public'));
app.set('views', __dirname + '/public/views');
app.set('view engine', 'ejs');
app.get('*', (req, res) => {
match({ routes, location: req.url }, (err, redirectLocation, props) => {
if (err) {
res.status(500).send(err.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (props) {
const markup = renderToString(<RoutingContext {...props} />);
res.render('index', { markup })
} else {
res.sendStatus(404);
}
});
});
const server = http.createServer(app);
server.listen(3000);
server.on('listening', () => {
console.log('Listening on 3000');
});
I've been using routes like the following, and I learned this via a great tutorial from Egghead.io
Live demo: http://ec2-52-91-0-209.compute-1.amazonaws.com
Demo repo: https://github.com/mikechabot/material-ui-hello-world
App.js (main entry)
import React from 'react';
import ReactDOM from 'react-dom';
import { Router } from 'react-router';
import routes from './config/routes.jsx';
ReactDOM.render(
<Router>{routes}</Router>,
document.getElementById('my-app')
);
./config/routes.jsx
import React from 'react';
import Main from '../components/Main';
import Index from '../components/Index';
import { Route, IndexRoute } from 'react-router';
export default (
<Route path="/" component={Main}>
<IndexRoute component={Index} />
</Route>
)
../components/Main.jsx
import React from 'react';
class Main extends React.Component {
render() {
return (
<div>
Hello World. Display my children:
{this.props.children}
</div>
);
}
}
export default Main;
../components/Index.jsx
import React from 'react';
class Index extends React.Component {
render() {
return (
<div>
I'm the index Route
</div>
);
}
}
export default Index ;

Resources