Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.location does not work with react app using react router 4

Have the following "app", and window.location is not configurable. I would ideally like to hit the submit button and go to an alternate domain.This is actually part of a larger app, in which I use an external Identity Provider for Signin. Snippet below is a fully working sample to illustrate the issue:

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import Router from './routes';
import reducers from './reducers';
import { BrowserRouter, Route, Switch } from 'react-router-dom';

const store = createStore(reducers);

class Signin extends React.Component {
  onSubmit() {
    var result = Object.getOwnPropertyDescriptor(window, 'location');
    window.location = 'http://www.bbc.co.uk';
  }

  render() {
    return (
      <div>
        <form
          className="form-horizontal col-xs-10 col-xs-offset-1"
          onSubmit={this.onSubmit.bind(this)}
        >
          <div className="form-group">
            <span className="col-xs-3" />
            <div className="col-sm-6">
              <button type="submit" className="btn btn-success btn-sm">
                Submit
              </button>
            </div>
          </div>
        </form>
        <script />
      </div>
    );
  }
}

class App extends React.Component {
  render() {
    return (
      <div>
        <div className="container">{this.props.children}</div>
      </div>
    );
  }
}

render(
  <Provider store={store}>
    <BrowserRouter>
      <App>
        <Switch>
          <Route exact path="/" component={Signin} />
        </Switch>
      </App>
    </BrowserRouter>
  </Provider>,
  document.getElementById('root')
);

You can see a working sample here

On my real "app", am using the following dependencies and versions:

 "prop-types": "^15.6.0"
  "react": "^15.6.1"
  "react-dom": "^15.6.1"
  "react-redux": "4.3.0"
  "react-router-dom": "^4.0.0"
  "react-scripts": "1.0.13"

You will notice I put in a line:

var result = Object.getOwnPropertyDescriptor(window, 'location');

This returns information about the location object and indicates configurable=false. The URL currently remains the same with a /? taggged on the end.

Can anyone offer any insights? Tried also window.location.href. It seems that perhaps react / react router is reconfiguring window.location to be un-configurable - is there any reason or workaround anyone can offer.

like image 810
arcseldon Avatar asked Mar 16 '26 16:03

arcseldon


1 Answers

OK, I wasn't suppressing the form submission.

 onSubmit(e) {
    e.preventDefault();
    window.location = "https://www.bbc.co.uk";
  }

Yes, that one again...

like image 166
arcseldon Avatar answered Mar 18 '26 07:03

arcseldon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!