0

I'm trying to load a new component i have made when i press the button 'Enter'. I have implemented the onKeyPress function successfully as follows.

    class SearchBar extends Component {

        constructor(props) {

            super(props);

            this.handleKeyPress = this.handleKeyPress.bind(this);

        }

        handleKeyPress(e) {

            if (e.key === 'Enter') {

                console.log("load new page");

            }
        }

        render() {

            return (

                <div className="search_bar_div">

                    <div className="search_bar_internal_div">

                        {/* Search bar for desktop and tablets */}
                        <span className="Rev_logo">Revegator</span>

                        <input type="search" placeholder="Search here" className="search_bar"
                               onKeyPress={this.handleKeyPress}/>

                    </div>

                 </div>
}

I get the console log correctly but the problem i have is how to load the component when i press Enter.

CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182

3 Answers3

1

What you can do is to establish Routes for you component and then dynamically change the route with this.context.router like

class SearchBar extends Component {

        constructor(props) {

            super(props);

            this.handleKeyPress = this.handleKeyPress.bind(this);

        }


        handleKeyPress(e) {

            if (e.key === 'Enter') {

                console.log("load new page");
                this.context.router.push('/home');
            }
        }

        render() {

            return (

                <div className="search_bar_div">

                    <div className="search_bar_internal_div">

                        {/* Search bar for desktop and tablets */}
                        <span className="Rev_logo">Revegator</span>

                        <input type="search" placeholder="Search here" className="search_bar"
                               onKeyPress={this.handleKeyPress}/>

                    </div>

                 </div>
              )
       }
}


SearchBar.contextTypes = {
  router: React.PropTypes.func.isRequired
};
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • i get a error as error 'PropTypes' is not defined – CraZyDroiD Apr 03 '17 at 06:11
  • There's an improvement in your updated answer. When i enter, i can see the url changes. But the page won't load. If i refresh the page manually, the related page will load. How do i fix this? – CraZyDroiD Apr 03 '17 at 06:52
  • Yes i was able to solve this problem by adding this line in my handleKeyPress method. 'location.reload();' – CraZyDroiD Apr 03 '17 at 11:27
0

How are you handling the route in your Router definitions in the index? It might not work if you don't have a parent route that then renders it's children, rather than having just a bunch of named routes.

Michael Myers
  • 133
  • 1
  • 10
0

for example, my index would look like this

ReactDOM.render(
  <Provider store={store}>
    <Router history={browserHistory}>
      <Route path='/' component={App}>
        <IndexRoute component={Welcome} />
        <Route path='/signin' component={SignIn} />
        <Route path='/signout' component={SignOut} />
        <Route path='/register' component={SignUp} />
        <Route path='/map' component={Map} />
        <Route path='/dashboard' component={RequireAuth(Dashboard)} />
        <Route path='/admin' component={RequireAdmin(Admin)} />
        <Router path='*' component={Welcome} />
      </Route>
</Router>

with my main App component (the top level Route under Router)

render() {
return (
  <div>
  <Header />
    {this.props.children}
  </div>
)
}

this means that '/' is rendered from the App component, and any route declared after the '/' will be rendered INTO the app component, instead of it's own page.

Michael Myers
  • 133
  • 1
  • 10