无法使用 REACT-ROUTER-DOM 重定向
Can't redirect with REACT-ROUTER-DOM
我有这个搜索方法,我想在搜索后重定向用户。问题是它什么也没做,我不知道为什么。 this.props.history 已定义,但对象 "history" 的 "location" 属性 似乎没有根据我在 push 方法的参数中输入的内容进行相应更改('./connexion' ) ... URL 也没有改变。搜索方法是绑定的,我使用 export default withRouter(SearchBar);
通过道具访问历史记录。知道我在其他文件中使用完全相同的东西(this.props.history.push() 和 withRouter)并且它工作正常......我使用 "react-router-dom": "^4.1.1"
和 browserHistory.
search(event) {
if (this.state.location === null) {
this.setState({ geosuggestId: 'geosuggest-input-alert' });
} else if (this.state.subjects.length === 0) {
this.setState({ matieresButtonId: 'matieres-button-alert' });
} else {
console.log(this.props.parent);
if (this.props.parent === 'Homepage') {
console.log(this.props.history);
this.props.history.push('/connexion');
}
}
}
完整文件:
import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import Geosuggest from 'react-geosuggest';
import SearchBySubjectsModal from './modals/search_by_subjects_modal';
import { withRouter } from 'react-router-dom';
/**
* Search bar for parent to search for profs
*/
class SearchBar extends Component {
constructor(props) {
super(props);
this.state = {
location: null,
subjects: [],
level: 'Collège',
matieresButtonId: 'matieres-button',
geosuggestId: 'geosuggest-input'
}
this.onSuggestSelect = this.onSuggestSelect.bind(this);
this.setSubjects = this.setSubjects.bind(this);
this.search = this.search.bind(this);
}
/**
* if the state for subjects and location is not null, then stop fields warning
*/
componentDidUpdate() {
if (this.state.subjects.length > 0) {
if (this.state.matieresButtonId !== 'matieres-button')
this.setState({ matieresButtonId: 'matieres-button' });
}
if (this.state.location !== null) {
if (this.state.geosuggestId !== 'geosuggest-input')
this.setState({ geosuggestId: 'geosuggest-input' });
}
}
/**
* set the state when choosing a location
* @param {*} suggest
*/
onSuggestSelect(suggest) {
this.setState({ location: suggest });
}
/**
* set the state when choosing subjects
* @param {*} suggest
*/
setSubjects(subjects, level) {
this.setState({ subjects, level });
}
/**
* Search method
* Check if subjects or location are null (is so, show warnings)
* If no warnings, perform search and redirect to search page
* @param {*} event
*/
search(event) {
if (this.state.location === null) {
this.setState({ geosuggestId: 'geosuggest-input-alert' });
} else if (this.state.subjects.length === 0) {
this.setState({ matieresButtonId: 'matieres-button-alert' });
} else {
console.log(this.props.parent);
if (this.props.parent === 'Homepage') {
console.log(this.props.history);
this.props.history.push('/connexion');
}
}
}
/**
* Uses GeoSuggest (google places api) to choose a town
* Uses Search By Subject modal to choose subjects
*/
render() {
return (
<div className="container" id="search-bar" >
<div className="text-center">
<form action="">
<div className="row">
<div className="col">
<Geosuggest
queryDelay={150}
autoActivateFirstSuggest={true}
inputClassName={this.state.geosuggestId}
placeholder="Où ?"
country="fr"
onSuggestSelect={this.onSuggestSelect} />
</div>
<div className="col">
<Link to="/">
<button data-toggle="modal" data-target=".choose-subject-modal" className="btn clickable" id={this.state.matieresButtonId}>
<i className="fa fa-graduation-cap"></i> Matières ?
</button>
</Link>
</div>
<div className="col">
<Link to="/">
<button type="submit" className="btn clickable" id="search-button" onClick={this.search}>
<h5 id="search-btn-txt"><i className="fa fa-search"></i> Trouver</h5>
</button>
</Link>
</div>
</div>
</form>
<SearchBySubjectsModal search={this.search} location={this.state.location} setSubjects={this.setSubjects} />
</div>
</div>
);
};
}
export default withRouter(SearchBar);
非常感谢!
我假设您使用的是 react-router v4。这个版本的 React Router 已经转向比以前更具声明性的方法。这种转变如何影响重定向?最简单的重定向方法是渲染 <Redirect>
React 路由器组件。那么整个事情将如何运作?您的搜索完成后会将状态部分 searchResult:
从 false 更改为例如结果列表。这会导致重新渲染。当您的组件在 state.searchResult 中找到非假值时,它们将呈现 <Redirect>
指向您的搜索结果路由的组件。
我一直对状态处理含糊不清,因为在 React 应用程序中有很多处理状态的方法。
您可以看看 authentication example in react router docs,它做的事情与您需要的几乎相同(它只执行验证而不是搜索)
我有这个搜索方法,我想在搜索后重定向用户。问题是它什么也没做,我不知道为什么。 this.props.history 已定义,但对象 "history" 的 "location" 属性 似乎没有根据我在 push 方法的参数中输入的内容进行相应更改('./connexion' ) ... URL 也没有改变。搜索方法是绑定的,我使用 export default withRouter(SearchBar);
通过道具访问历史记录。知道我在其他文件中使用完全相同的东西(this.props.history.push() 和 withRouter)并且它工作正常......我使用 "react-router-dom": "^4.1.1"
和 browserHistory.
search(event) {
if (this.state.location === null) {
this.setState({ geosuggestId: 'geosuggest-input-alert' });
} else if (this.state.subjects.length === 0) {
this.setState({ matieresButtonId: 'matieres-button-alert' });
} else {
console.log(this.props.parent);
if (this.props.parent === 'Homepage') {
console.log(this.props.history);
this.props.history.push('/connexion');
}
}
}
完整文件:
import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import Geosuggest from 'react-geosuggest';
import SearchBySubjectsModal from './modals/search_by_subjects_modal';
import { withRouter } from 'react-router-dom';
/**
* Search bar for parent to search for profs
*/
class SearchBar extends Component {
constructor(props) {
super(props);
this.state = {
location: null,
subjects: [],
level: 'Collège',
matieresButtonId: 'matieres-button',
geosuggestId: 'geosuggest-input'
}
this.onSuggestSelect = this.onSuggestSelect.bind(this);
this.setSubjects = this.setSubjects.bind(this);
this.search = this.search.bind(this);
}
/**
* if the state for subjects and location is not null, then stop fields warning
*/
componentDidUpdate() {
if (this.state.subjects.length > 0) {
if (this.state.matieresButtonId !== 'matieres-button')
this.setState({ matieresButtonId: 'matieres-button' });
}
if (this.state.location !== null) {
if (this.state.geosuggestId !== 'geosuggest-input')
this.setState({ geosuggestId: 'geosuggest-input' });
}
}
/**
* set the state when choosing a location
* @param {*} suggest
*/
onSuggestSelect(suggest) {
this.setState({ location: suggest });
}
/**
* set the state when choosing subjects
* @param {*} suggest
*/
setSubjects(subjects, level) {
this.setState({ subjects, level });
}
/**
* Search method
* Check if subjects or location are null (is so, show warnings)
* If no warnings, perform search and redirect to search page
* @param {*} event
*/
search(event) {
if (this.state.location === null) {
this.setState({ geosuggestId: 'geosuggest-input-alert' });
} else if (this.state.subjects.length === 0) {
this.setState({ matieresButtonId: 'matieres-button-alert' });
} else {
console.log(this.props.parent);
if (this.props.parent === 'Homepage') {
console.log(this.props.history);
this.props.history.push('/connexion');
}
}
}
/**
* Uses GeoSuggest (google places api) to choose a town
* Uses Search By Subject modal to choose subjects
*/
render() {
return (
<div className="container" id="search-bar" >
<div className="text-center">
<form action="">
<div className="row">
<div className="col">
<Geosuggest
queryDelay={150}
autoActivateFirstSuggest={true}
inputClassName={this.state.geosuggestId}
placeholder="Où ?"
country="fr"
onSuggestSelect={this.onSuggestSelect} />
</div>
<div className="col">
<Link to="/">
<button data-toggle="modal" data-target=".choose-subject-modal" className="btn clickable" id={this.state.matieresButtonId}>
<i className="fa fa-graduation-cap"></i> Matières ?
</button>
</Link>
</div>
<div className="col">
<Link to="/">
<button type="submit" className="btn clickable" id="search-button" onClick={this.search}>
<h5 id="search-btn-txt"><i className="fa fa-search"></i> Trouver</h5>
</button>
</Link>
</div>
</div>
</form>
<SearchBySubjectsModal search={this.search} location={this.state.location} setSubjects={this.setSubjects} />
</div>
</div>
);
};
}
export default withRouter(SearchBar);
非常感谢!
我假设您使用的是 react-router v4。这个版本的 React Router 已经转向比以前更具声明性的方法。这种转变如何影响重定向?最简单的重定向方法是渲染 <Redirect>
React 路由器组件。那么整个事情将如何运作?您的搜索完成后会将状态部分 searchResult:
从 false 更改为例如结果列表。这会导致重新渲染。当您的组件在 state.searchResult 中找到非假值时,它们将呈现 <Redirect>
指向您的搜索结果路由的组件。
我一直对状态处理含糊不清,因为在 React 应用程序中有很多处理状态的方法。
您可以看看 authentication example in react router docs,它做的事情与您需要的几乎相同(它只执行验证而不是搜索)