简体   繁体   English

React Redux表单2使用onSubmit和redirect提交

[英]React Redux form 2 submits with onSubmit and redirect

I'm trying to improve an app with weather and polution. 我正在尝试用天气和污染来改进应用程序。 The main idea is to have 2 submit buttons with redirects to other url's. 主要的想法是有2个提交按钮,重定向到其他网址。 I've tried some options, the last as you see was about making 2 onClick functions, but I can't pass the data to onFormSubmit function. 我已经尝试了一些选项,最后你看到的是关于制作2个onClick函数,但是我无法将数据传递给onFormSubmit函数。

import React, {Component} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {fetchWeather} from '../actions/index';
import { Link } from 'react-router';
import { reduxForm } from 'redux-form';

class SearchBar extends Component{
    constructor(props){
        super(props);

        this.state = {term:''};

        this.onInputChange= this.onInputChange.bind(this);
        this.onFormSubmit = this.onFormSubmit.bind(this);
    }
static contextTypes = {
    router: React.PropTypes.object
  };

onInputChange(event){

    this.setState({term : event.target.value});
};




onFormSubmit(event){
    event.preventDefault();

if (this.numberButton == 1){
return (this.props.fetchWeather(this.state.term)
    .then(() => {
     this.context.router.push('/')}));
} else if (this.numberButton == 2){

} else {
    return console.log('Went wrong');
}



    this.setState({term:''});

};


    render(){
        return (
            <div>
            <form 
          onSubmit={this.onFormSubmit} 
            className="input-group">
                <input 
                placeholder="Put here a US city name"
                className="form-control"
                value={this.state.term}
                onChange={this.onInputChange}
                 />
                <span className="input-group-btn" role="group">
                        <button type="submit" name="button1" value="Weather" className="btn btn-secondary btn-success" onClick={ () => a=1} >Weather</button>
                        <button type="submit" name="button2" value="Polution" className="btn btn-secondary btn-info" onClick={() =>  a =2} >Polution</button>
                    </span>

                </form>

                </div>
        )
    }

}

function mapDispatchToProps(dispatch){
    return bindActionCreators({fetchWeather}, dispatch);
}

export default connect(null, mapDispatchToProps)(SearchBar);  

There are two different ways that you can fix this: 有两种不同的方法可以解决这个问题:

1) Put an id on each button (eg 'weather-button'), then check e.target.id === 'weather-button' instead of this.numberButton == 1. I don't see this.numberButton declared anywhere, so that condition will never be true. 1)在每个按钮上放置一个id(例如'weather-button'),然后检查e.target.id ==='weather-button'而不是this.numberButton == 1.我没有看到this.numberButton声明在任何地方,所以这种情况永远不会成真。

2) Remove the form wrapper altogether, and provide a different handleClick to each button. 2)完全删除表单包装,并为每个按钮提供不同的句柄。

handleWeatherClick = e => { ... }
handlePollutionClick = e => { ... }

...

<span className="input-group-btn" role="group">
    <button className="btn btn-secondary btn-success" onClick={this.handleWeatherClick}>
        Weather
    </button>
    <button className="btn btn-secondary btn-info" onClick={this.handlePollutionClick}>
        Pollution
    </button>
</span>

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

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