[英]My react route is redirecting to localhost:3000 and not the page it should redirect to
I have 2 components both are exactly the same.我有 2 个组件都完全相同。 one is redirected to when I click on a Navlink inside of my navbar that I created using react-bootstrap.当我单击使用 react-bootstrap 创建的导航栏中的 Navlink 时,将重定向到一个。 The other component that is exactly the same just redirects to localhost:3000 and not "./member" when I click on the html button that should redirect to that component.当我单击应该重定向到该组件的 html 按钮时,完全相同的另一个组件只是重定向到 localhost:3000 而不是“./member”。 Please help me.请帮我。
the html button and the function to redirect look like html 按钮和 function 重定向看起来像
import {Link, Route, withRouter, useHistory} from 'react-router-dom'
const Posts = (props) => {
const dispatch = useDispatch();
const history = useHistory();
const getProfile = async (member) => {
// const addr = dispatch({ type: 'ADD_MEMBER', response: member })
console.log(member)
history.push('/member')
}
return (
<div>
<button onClick={() => getProfile(p.publisher)}>Profile</button>
</div>
)
}
export default withRouter(Posts);
The routes.js looks like routes.js 看起来像
const Routes = (props) => {
return (
<Switch>
<Route path="/member" exact component={Member} />
</Switch>
)
}
export default Routes
The component that I am trying to redirect to is exactly the same as one that is redirected to and working when I click on it from the navlink.我尝试重定向到的组件与当我从导航链接单击它时重定向到并正在工作的组件完全相同。 I have downgraded to history 4.10.1我已经降级到历史 4.10.1
My index.js is我的 index.js 是
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Router, Route } from 'react-router-dom';
import * as history from 'history';
import * as serviceWorker from './serviceWorker';
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import rootReducer from './reducers'
const store = createStore(rootReducer)
const userHistory = history.createBrowserHistory();
ReactDOM.render(
<Provider store = {store}>
<Router history={userHistory}>
<BrowserRouter>
<Route component={App} />
</BrowserRouter>
</Router>
</Provider>,
document.getElementById('root'));
serviceWorker.unregister();
When I wrap the app route in the url goes to./member but it does not load.当我将应用程序路由包装在 url 中时,会转到./member 但它不会加载。
<Switch>
<Route path="/" exact component={app} />
<Route path="/member" component={Member} />
</Switch>
<button onClick={() => history.push(`/${p.publisher}`)}>Profile</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.