成功重定向后,React不呈现组件

时间:2018-09-04 11:35:25

标签: javascript reactjs react-router-v4

我是新来的反应者,曾尝试编写一些代码,以便用户在成功登录后尝试导航到另一个页面。但是,我看到重定向发生在Home组件上,但是该页面至少没有发送我的log语句。看到白页。我的组件如下图所示。

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';


import {
  Router,
  Route,
} from "react-router-dom";
import Home from './Home';
import history from './History';

ReactDOM.render(
  <Router history={history}>
  <div>
    <Route path="/" exact component={App} />
    <Route path="home" exact component={Home} />
  </div>
  </Router>,
  document.getElementById('root'));

registerServiceWorker();

App.js

import React, { Component } from 'react';
import axios from 'axios';
import history from './History';


class App extends Component {

constructor(){
    super();
    this.state = {
      username: '',
      password: '',
      userdata: [],
      isLogin: false
    };

  this.handleUserChange = this.handleUserChange.bind(this);
  this.handleSubmit = this.handleSubmit.bind(this);
}

handleSubmit(event){
    event.preventDefault();
    axios.post('https://api.github.com/user',{}, {
      auth: {
        username: this.state.username,
        password: this.state.password
      }
    }).then((response) => {
      console.log(response.data);
      this.setState({
        userdata: response.data,
        isLogin:true
      });
    }).catch(function(error) {
      console.log('Error on Authentication' + error);
  });
}

handleUserChange(event){
    this.setState({
      username : event.target.value,
    });
}

handlePasswordChange = event => {
  this.setState({
    password: event.target.value
  });
}

render() {
    if(this.state.isLogin){
     history.push({pathname: 'home', state: this.state.data});
     history.go('home');
    //browserHistory.push('home');
    }
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          username :
          <input type="text" value={this.state.username} onChange={this.handleUserChange} required/>
        </label>
        <label>
          password :
          <input type="password" value={this.state.password} onChange={this.handlePasswordChange} required/>
        </label>
        <input type="submit" value="LogIn" />
      </form>

    );
  }
}

export default App;

Home.js

import React, { Component } from 'react';
import axios from 'axios';

class Home extends Component {

  constructor(props){
    super(props);
    this.state = {
        Search:'',
        results:[]
      }

  }

  getInfo = () => {

    axios.get('https://api.github.com/search/users',{
    params: {
      q: this.state.Search,
      in:'login',
      type:'Users'
    }
  }).then(({ response }) => {
        this.setState({
          results: response.data.items.login
        })
      })
  }

  handleSearchChange(event){
    this.setState({
      Search: this.state.Search
    }, setInterval(() => {
      if (this.state.Search && this.state.Search.length > 1) {
        if (this.state.Search.length % 2 === 0) {
          this.getInfo();
        }
    }
  },500));

  }

  render(){
    console.log("Home page : " + this.props.data);
  return (
    <div>
      Home page {this.props.data}

    <form>
      <label>
        Search :
        <input type="text" placeholder="Search for..." value={this.state.Search} onChange={this.handleSearchChange} />
      </label>
    </form>
     </div>
  );
}
}

export default Home;

我在这里犯什么错误有帮助吗?

谢谢

2 个答案:

答案 0 :(得分:3)

您缺少/,因此路线应如下所示。

    <Route path="/home" exact component={Home} />

并按住

history.push({pathname: '/home', state: this.state.data});

答案 1 :(得分:0)

history.push({pathname: '/home', state: this.state.data});<Route path="/home" exact component={Home} />

缺少/