【问题标题】:React js: react router not redirectingReact js:反应路由器不重定向
【发布时间】:2021-05-13 16:42:39
【问题描述】:

我是新手学习反应,我不确定为什么这不能正常工作。 当我按“Enter”时,我想重定向到 /search,但由于某种原因它不起作用。 重定向到不同页面的正确方法是什么?

function App(){

    const [inputVal, setInputVal] = useState('');
    const [submitted, setSubmitted] = useState(false);

    function handleInput(e) {
        const query = e.target.value;
        setInputVal(query);
    }

    function handleKeyDown(e) {
        if (e.key === 'Enter') {
            setSubmitted(true);
            console.log(submitted);
            console.log(inputVal);
        }
    }

    return (
        <div className='App'>
            <div>
                <h1>Movie</h1>
                <input
                    onKeyDown={handleKeyDown}
                    onChange={handleInput}
                    placeholder='Search...'
                />
            </div>

            <Router>
                <Switch>
                    <Route path='/' exact component={Home} />
                    {submitted && <Redirect to='/search' />}
                    <Route path='/search' component={Search} />
                    <Route path='/details/:id' component={Detail} />
                </Switch>
            </Router>
        </div>
    );
}

【问题讨论】:

标签: reactjs redirect react-router


【解决方案1】:

我建议使用历史记录以编程方式导航到搜索。您可能希望将 inputVal 作为查询参数传递,以便您可以在该组件中访问它。

import { useHistory } from 'react-router-dom';

function App() {
    const history = useHistory();
    const [inputVal, setInputVal] = useState('');

    function handleInput(e) {
        const query = e.target.value;
        setInputVal(query);
    }

    function handleKeyDown(e) {
        if (e.key === 'Enter') {
            // You can put the inputVal as a query parameter
            history.push(`/search?search=${inputVal}`);
        }
    }

    return (
        <div className="App">
            <div>
                <h1>Movie</h1>
                <input
                    onKeyDown={handleKeyDown}
                    onChange={handleInput}
                    placeholder="Search..."
                />
            </div>

            <Router>
                <Switch>
                    <Route path="/" exact component={Home} />
                    <Route path="/search" component={Search} />
                    <Route path="/details/:id" component={Detail} />
                </Switch>
            </Router>
        </div>
    );
}

【讨论】:

  • 感谢您加倍努力!不幸的是,它对我不起作用。我收到错误类型错误:历史未定义。我使用的路由器是 V 5.2.0。我查看了错误,似乎最新版本中不包含 useHistory。我可能不得不降级到 v3 或我不知道如何大声笑
  • reactrouter.com/web/api/Hooks/usehistory 应该是最新版本。请参阅此处的文档。
  • 还是没用。看看这个,我仍然得到相同的结果。 stackoverflow.com/questions/66155448/…
【解决方案2】:

它总是选择你的开关中的第一个,所以因为你在主页上,它可能永远不会看到下面的任何东西,这就是它不会重定向你的原因。您要么需要将重定向放在主路由上方,要么尝试使用 react-router 中的“历史”功能,这需要您在根级别设置它才能使用,但您可以这样做

function handleKeyDown(e) {
    if (e.key === 'Enter') {
        history.push('/search')
        console.log(submitted);
        console.log(inputVal);
    }
}

【讨论】:

  • 刚刚发现useHistory和最新的路由器版本没有可比性。我什至不知道如何解决这个问题了。我被困了1天。我想我可能已经降级路由器版本以使其正常工作。
猜你喜欢
  • 2020-10-22
  • 1970-01-01
  • 2021-09-29
  • 2021-09-06
  • 1970-01-01
  • 2021-03-02
  • 2021-05-15
  • 1970-01-01
  • 2019-01-19
相关资源
最近更新 更多