7

I'm new to react and i'm trying to load another page when user clicks a button. I have used window.location but nothing happens when i click the button. How to fix this?

This where i have included my onClick

<div>
  <img className="menu_icons" src={myhome}/>
  <label className="menu_items" onClick={home}>Home</label>
</div>

Function written for onClick

function home(e) {
    e.preventDefault();
    window.location = 'my-app/src/Containers/HomePage.jsx';
}
2
  • 2
    are you using react router ?? Commented Feb 20, 2017 at 5:56
  • Did you find solution for the question you asked? Commented Feb 23, 2017 at 6:29

3 Answers 3

18

If you really want to build a single app I'd suggest using React Router. Otherwise you could just use plain Javascript:

There are two main ways of doing depending on your desire you want to:

  1. Style an <a> as your button
  2. Use a <button> and redirect programatically

Since according to your question my assumption is that you're not building a single page app and using something along the lines of React router. And specifically mentioned use of button Below is a sample code

var Component = React.createClass({
  getInitialState: function() { return {query: ''} },
  queryChange: function(evt) {
    this.setState({query: evt.target.value});
  },
  handleSearch: function() {
    window.location.assign('/search/'+this.state.query+'/some-action');
  },
  render: function() {
    return (
      <div className="component-wrapper">
        <input type="text" value={this.state.query} />
        <button onClick={this.handleSearch()} className="button">
          Search
        </button>
      </div>
    );
  }
});

Mistakes I see according to your post

  1. The function which uses window.location is not properly called in render method
  2. You could use window.location.assign() method which helps to load new document
  3. I doubt that JSX pages are rendered directly in browser level when you try to call them directly

Hope this would help, if not and you figure out an answer please share

Sign up to request clarification or add additional context in comments.

Comments

6

You need to bind the handler in the constructor of your component, otherwise React won't be able to find your home function.

constructor(props) {
  super(props);
  this.home = this.home.bind(this);
}

Comments

1

You need to reference the method using this, otherwise React does not find it.

<div>
  <img className="menu_icons" src={myhome}/>
  <label className="menu_items" onClick={this.home}>Home</label>
</div>

3 Comments

No it won't work if i include 'this.' I have tested with an alert. It shows up without 'this.'
Can you post what you have in full? I just need to see your component.
It won't work if you try to redirect to a jsx page which is not recognized by browsers. It would have to be an html page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.