反应句柄表单提交

时间:2016-07-08 08:27:07

标签: javascript forms reactjs react-redux

我正在尝试在React / Redux中创建一个表单。现在我只希望表单在提交表单时触发我的函数handleSubmit。但是目前看起来该功能在页面加载时立即被触发......

 My Query:
 SELECT Case When msa_high>=50 AND msa_high<=60 then 'low'
             When msa_high>60 AND msa_high>=70 then 'medium'
             When msa_high>70 then 'high' end as edu_lv , Sum(rev)/Sum(S_days) AS avg_sales                                        
 from store_msa left join(Select
 (EXTRACT(YEAR FROM t.saledate)||'-'||
 EXTRACT(MONTH FROM t.saledate) ||'-'||
 t.store ) AS mys,COUNT(DISTINCT t.saledate) AS S_days, sum(t.amt) AS rev
 From trnsact t
 Where (t.saledate)<'2005-08-01' AND t.stype='P' AND mys IN
(SELECT
(EXTRACT(YEAR FROM t.saledate)||'-'||
EXTRACT(MONTH FROM t.saledate) ||'-'||
t.store) AS mys
FROM trnsact t
HAVING COUNT(DISTINCT t.saledate)>20
GROUP BY mys,t.store)) AS rev
ON store_msa.store=rev.store
Group By edu_lv,avg_sales
Order By avg_sales;

我知道剩下的代码还不完全正确但我现在主要担心的是在正确的时刻触发onSubmit操作。

提前致谢!

2 个答案:

答案 0 :(得分:25)

您似乎没有绑定this.handleSubmit = this.handleSubmit.bind(this);

来自the docs

  

方法遵循与常规ES6类相同的语义,这意味着   它们不会自动将其绑定到实例。

你有三个选择

  1. 添加构造函数并在那里进行绑定(推荐):

    onSubmit={this.handleSubmit.bind(this)}

  2. 直接绑定:

    =>

  3. 使用箭头onSubmit={() => this.handleSubmit}函数

    df['new_index'] = np.arange(df.shape[0]) df = df.set_index('new_index') df['col1'].get(2, 'the_default_value')

答案 1 :(得分:-1)

您需要将handleSubmit作为道具

传递
 <SubmitButton value='Submit' onSubmit={this.handleSubmit.bind(this)}/>

然后将该prop指定给SubmmitButton组件的render函数上的按钮。

// submmit button component @render method

<button onClick={this.props.onSubmit} >Submit</button>