0

After successfull API invocation, I am calling window.onload for component refresh to redirect to the specific route.

export function _getUserPlanForUser(data) {
let token = localStorage.getItem('accessToken');
 let url = "/userplans/listuserplans?access_token=" + token;
 let BASE = "http://172.104.176.144:9080"
 let actualurl = BASE + url;

 Api._callAPI(actualurl, 'GET', data, (type, dt) => {
     if (type == 'success') {
         dispatcher.dispatch({
             type: 'UsersPlansForUser',
             data: dt,
         })
         window.onload = function() {
             if(!window.location.hash) {
                 window.location = window.location + '#loaded';
                 window.location.reload(true);
             }
         }

     }
 });

}

And in the routes.js file I am validating based on condition

import React from 'react';
 import { Route, IndexRoute } from 'react-router';
 import App from './components/App';
 import loginPage from './components/login/login.component.jsx';  
 import dashboard from './components/dashboard';
 import Home from './components/Home/home.jsx';
import Myplan from './components/MyPlan/myplan.jsx';

export default (
    <Route component={App}>
        <Route path="/" >
            <IndexRoute component={loginPage} />
         </Route>
        <Route path="/dashboard" component={dashboard} >
            {
               (localStorage.getItem("ValidateRoute")) == "0" ? 
              [
                 <IndexRoute component={Myplan} />,
                  <Route path='/home' component={Home} />
              ] : [
                <IndexRoute component={Home} />,
                <Route path='/myplan' component={Myplan} />                 
              ]
           }
        </Route>
    </Route>
  );

Based on condition ValidateRoute I am routing to specific component after a successfull reload! Everything working fine on Chrome, but as window.reload is not working it is not redirecting to the specific route!

Please give me some guidelines for reload on Mozilla (or) proper redirection in Mozilla to the particular route.

I've been working for 1 week and still cannot find any soulution. Issue fixed would be very helpfull to me.

Binar Web
  • 867
  • 1
  • 11
  • 26
Siva Sai
  • 319
  • 2
  • 5
  • 21
  • Use window.replace if you must use the code you have. The statement after `window.location = window.location + '#loaded';` will never execute - I will however strongly suggest you redesign. React does not need plain JS to change routes. It seems to me you are not using the power of the framework you are using - https://stackoverflow.com/questions/31079081/programmatically-navigate-using-react-router – mplungjan Apr 30 '18 at 07:07
  • @mplungjan normally wont use plain Js at all but for this routing i cannot find any other way for routing, AFter successfull api call based on the response length i need to redirect to particular component as showed in the routing file can you suggest any other way. Thanks for quick reply – Siva Sai Apr 30 '18 at 07:16
  • I would really try to find a react way - did you read the link I gave? I would think a history.push or similar was the way to go. Others may have better ideas. – mplungjan Apr 30 '18 at 07:19
  • window.replace is not making component refresh so it is not redirecting to proper component once after reload of the page it is properly redirecting to specific component so i supposed to use that. – Siva Sai Apr 30 '18 at 07:23
  • I used history.push also initially it is redirecting to the home component only! if the condition matches also not redirecting to myplan component – Siva Sai Apr 30 '18 at 07:25

0 Answers0