简体   繁体   English

REACT this.props.history.push不重定向

[英]REACT this.props.history.push does not redirect

I've got this search method where I want to redirect the user after searching. 我有这种搜索方法,我想在搜索后重定向用户。 The issue is that it does not do anything and I don't know why. 问题是它没有做任何事情,我不知道为什么。 this.props.history IS defined but the "location" property of the object "history" does not seem to change accordingly to what i put in the parameters of the push method ('./connexion') ... The search method IS binded and I use export default withRouter(SearchBar); this.props.history已定义,但对象“history”的“location”属性似乎没有相应于我在push方法的参数中放入的内容('./connexion')...搜索方法IS binded我使用export default withRouter(SearchBar); to access the history via props 通过道具访问历史

 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');
        }
    }
}

Full file : 完整档案:

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);

Thank you for your answers :) 谢谢您的回答 :)

try use just: hashHistory.push('/your/path'); 尝试使用just: hashHistory.push('/your/path');

And in ur head: import { hashHistory } from 'react-router'; 在你的头脑中: import { hashHistory } from 'react-router';

Your way is correct for react router v4. 反应路由器v4的方式是正确的。 withRouter does expose the history object to the wrapped component. withRouter会将历史对象公开给包装组件。 Just to make sure you create the history object and feed that to the router component. 只是为了确保您创建历史对象并将其提供给路由器组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM