2016-07-14 51 views
2

我按照教程&他們使用event.preventDefault()上的Save按鈕&保存表格到狀態。我還沒有真正寫過input標籤,但到目前爲止我已經添加了保存按鈕,它有點像重新加載它不應該做的頁面。event.preventDefault未在React中工作

這是我的頁面組件:

class manageLocationPage extends React.Component { 
    constructor(props, context) { 
     super(props, context); 
     this.state = { 

     }; 
     this.SaveLocation = this.SaveLocation.bind(this); 
    } 

    componentWillMount() { 

    } 

    componentDidMount() { 

    } 

    SaveLocation(event) { 
     event.preventDefault(); 
     console.log("Saved"); 
    } 

    render() { 
     return (
      <div> 
       <LocationForm listingData={this.props.listingData} onSave={this.SaveLocation}/> 
      </div> 
     ); 
    } 
} 

locationForm組件:

const LocationForm = ({listingData, onSave, loading, errors}) => { 
    return (
     <form> 
      <h1>Add/Edit Location</h1> 
      <TextInput /> 

     {/*Here below is where we submit out input data*/} 
      <input type="submit" disabled={loading} value={loading ? 'Saving...' : 'Save'} className="buttonSave" onClick={onSave}/> 
     </form> 
    ); 
}; 

我錯過了什麼?

+0

如果你使用'form'的'onSubmit'呢? – zerkms

回答

3

相反,它的onClick你input.submit的,你應該做的

const LocationForm = ({listingData, onSave, loading, errors}) => { 
    return (
     <form onSubmit={onSave}> 
      <h1>Add/Edit Location</h1> 
      <TextInput /> 

     {/*Here below is where we submit out input data*/} 
      <input type="submit" disabled={loading} value={loading ? 'Saving...' : 'Save'} className="buttonSave"/> 
     </form> 
    ); 
}; 

因此事件是被阻止表單提交。

https://facebook.github.io/react/docs/tutorial.html#submitting-the-form

+0

此外,我沒有包括'加載'道具這就是爲什麼它導致頁面重新加載行爲。我已經修復了這個問題,這個方法也可以。 –

相關問題