3

I need to change the address in browser when the user submits the search query, based on that query (something familiar to what Google does). When I do this, the address changes properly, but it redirects me to my 404 route. And when I manually input text to the address bar, I can see the proper page.

This is a part of my routes:

...
<Route path="some-address" component={MyComponent}>
    <Route  path=":query" />
</Route>
...

Here's a simplified version of my component:

@withRouter
@connect(...)
export default class MyComponent extends Component {
    ...
    render() {
        return (
            <form onSubmit={(e) => {
                e.preventDefault(); 
                this.props.router.push(e.target.myInput.value)}}
            >
                <input name="myInput" />
            </form>
    }
}

React Router is 2.6.1 version.

2 Answers 2

1

This is how I managed to solve this:

    @withRouter
    @connect(...)
    export default class MyComponent extends Component {
        ...
        render() {
            return (
                <form onSubmit={(e) => {
                    e.preventDefault(); 
                    this.props.router.push({
                        pathname: this.props.location.pathname,
                        query: e.target.myInput.value && {search: e.target.myInput.value}
                    })
                }}>
                    <input name="myInput" />
                </form>
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

use this syntax :

<Route path="/some-address" component={MyComponent}>
    <Route  path=":query" />
</Route>

Comments

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.