在 React 应用程序中更改商店的路线
Change route from store in React application
我有一个接收用户名和密码并将其发送以进行身份验证的 React 组件。如果身份验证成功,页面应移动到呈现另一个组件的不同路由。
现在的问题是,我不确定如何从我的商店更改路线?即不使用 React Router 的 <Link>
组件?我知道我们可以使用 this.props.history.push(/url)
以编程方式更改路由,但它只能在使用 <Route>
的 React 组件内部使用,不能在商店中使用。
我将 React16 与 MobX 一起用于状态管理,并使用 ReactRouter V4 进行路由。
商店:
class Store {
handleSubmit = (username, password) => {
const result = validateUser(username, password);
// if result === true, change route to '/search' to render Search component
}
}
登录组件:
const Login = observer(({ store }) => {
return (
<div>
<form onSubmit={store.handleSubmit}>
<label htmlFor="username">User Name</label>
<input type="text" id="username" onChange={e => store.updateUsername(e)} value={store.username} />
<label htmlFor="password">Password</label>
<input type="password" id="password" onChange={e => store.updatePassword(e)} value={store.password} />
<input type="submit" value="Submit" disabled={store.disableSearch} />
</form>}
</div>
)
})
主要应用组件:
import { Router, Route, Link } from "react-router-dom";
@observer
class App extends React.Component {
render() {
return (
<Router history={history}>
<div>
<Route exact path="/"
component={() => <Login store={store} />}
/>
<Route path="/search"
component={() => <Search store={store} />}
/>
</div>
</Router>
)
}
}
您可以在单独的模块中创建 history
,然后将其导入您的主应用程序组件和商店:
// history.js
import createHistory from 'history/createBrowserHistory';
const history = createHistory();
export default history;
// Store.js
import history from './history';
class Store {
handleSubmit = (username, password) => {
const result = validateUser(username, password);
if (result) {
history.push('/search');
}
}
}
// App.js
import { Router, Route, Link } from "react-router-dom";
import history from './history';
@observer
class App extends React.Component {
render() {
return (
<Router history={history}>
<div>
<Route exact path="/"
component={() => <Login store={store} />}
/>
<Route path="/search"
component={() => <Search store={store} />}
/>
</div>
</Router>
)
}
}
我有一个接收用户名和密码并将其发送以进行身份验证的 React 组件。如果身份验证成功,页面应移动到呈现另一个组件的不同路由。
现在的问题是,我不确定如何从我的商店更改路线?即不使用 React Router 的 <Link>
组件?我知道我们可以使用 this.props.history.push(/url)
以编程方式更改路由,但它只能在使用 <Route>
的 React 组件内部使用,不能在商店中使用。
我将 React16 与 MobX 一起用于状态管理,并使用 ReactRouter V4 进行路由。
商店:
class Store {
handleSubmit = (username, password) => {
const result = validateUser(username, password);
// if result === true, change route to '/search' to render Search component
}
}
登录组件:
const Login = observer(({ store }) => {
return (
<div>
<form onSubmit={store.handleSubmit}>
<label htmlFor="username">User Name</label>
<input type="text" id="username" onChange={e => store.updateUsername(e)} value={store.username} />
<label htmlFor="password">Password</label>
<input type="password" id="password" onChange={e => store.updatePassword(e)} value={store.password} />
<input type="submit" value="Submit" disabled={store.disableSearch} />
</form>}
</div>
)
})
主要应用组件:
import { Router, Route, Link } from "react-router-dom";
@observer
class App extends React.Component {
render() {
return (
<Router history={history}>
<div>
<Route exact path="/"
component={() => <Login store={store} />}
/>
<Route path="/search"
component={() => <Search store={store} />}
/>
</div>
</Router>
)
}
}
您可以在单独的模块中创建 history
,然后将其导入您的主应用程序组件和商店:
// history.js
import createHistory from 'history/createBrowserHistory';
const history = createHistory();
export default history;
// Store.js
import history from './history';
class Store {
handleSubmit = (username, password) => {
const result = validateUser(username, password);
if (result) {
history.push('/search');
}
}
}
// App.js
import { Router, Route, Link } from "react-router-dom";
import history from './history';
@observer
class App extends React.Component {
render() {
return (
<Router history={history}>
<div>
<Route exact path="/"
component={() => <Login store={store} />}
/>
<Route path="/search"
component={() => <Search store={store} />}
/>
</div>
</Router>
)
}
}