ReactJs React Redux Login issue reactjs react native,reactjs react native share code,reactjs react

I am learning react and redux and trying to implement login ..

below is my routing

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Login from './components/Login/Login'
import Home from './components/Home/Home'
import store from './store/store'
import {Provider}  from 'react-redux';
import history from './history/history';
import {Router,Route,Switch,Redirect} from 'react-router-dom'
import {connect} from 'react-redux'

class App extends Component {
  render() {
    let storelogginState=store.getState();
    let logginState=storelogginState.loginReducer.loggedIn
    console.log('store',logginState)
    return (
      <Router history={history}>
        <div className="App">
          <Provider store={store}>
            <Switch>
                <Route exact path="/" component={Login}/>
                <Route exact path="/home"  render={()=>(
                  this.props.loginReducer.loggedIn?(<Home/>):(<Redirect to="/"/>)
              )  }/>
            </Switch>
          </Provider>
        </div>
      </Router>

    );
  }
}

Now in login once i clicking on login i am dispatching an action

return (dispatch)=>{
    fetch('./data.json').then(response=>response.json()).then((data)=>{
        console.log('data'+data)
         history.push('/home')
        return dispatch(loginSuccess());
    })
  }
}

function loginSuccess(){
  return {
    type:'success',
     loginData:''
  }
}

and below is my reducer

const initalState={
  loggedIn:false
}
 function loginReducer(state=initalState,action){
  switch (action.type) {
    case 'success':
        return Object.assign({},state,{loggedIn:true});
      break;
    default:
    return  state;

  }

}

So my question is in Router

<Route exact path="/home"  render={()=>(
                  logginState?(<Home/>):(<Redirect to="/"/>)
              )  }/>

logginState is always false but in state its showing true once sucessfully logged in bacause of that i am able to go home page but its redirecting me back to login page.

Thanks

Answer:1



  1. reactjs react native
  2. reactjs react native share code
  3. reactjs react router
  4. reactjs react table
  5. reactjs react hooks
  6. reactjs react router dom
  7. reactjs react fragment
  8. reactjs react-router-dom example

I'm trying to import a javascript class with default export. But I cannot use the functions defined in it. An error says "Uncaught TypeError: _omise2.default.setPublicKey is not a function" Here is ...

I'm trying to import a javascript class with default export. But I cannot use the functions defined in it. An error says "Uncaught TypeError: _omise2.default.setPublicKey is not a function" Here is ...

im trying to make a seperate page / component for searching and am wondering how i would pass the state to a different component ? i want to copy and paste everything in the render/return part ...

im trying to make a seperate page / component for searching and am wondering how i would pass the state to a different component ? i want to copy and paste everything in the render/return part ...