2015-01-13 17 views
6

Al momento dell'invio del modulo, voglio mostrare "Please wait .." e all'invio riuscito dei dati restituiti dal server. Usando jQuery, è facile da fare. Ma dovrebbe esserci un modo React in quanto a React non piace un tale tipo di manipolazione DOM diretta - penso. 1) Ho ragione? 2) Come mostrare il messaggio in (non dopo) l'invio del modulo?react.js - mostra un messaggio su e dopo l'invio del modulo

var FormComp = React.createClass({ 

    handleSubmit:function(){ 

    var userName=this.refs.userName.getDOMNode().value.trim(); 
    var userEmail= this.refs.userEmail.getDOMNode().value.trim(); 

    if(!userName || !userEmail){ 

    return; 

    } 


    this.props.onFormSubmit({userName:userName, userEmail:userEmail,url:"/api/submit"}); 

    this.refs.userName.getDOMNode().value=''; 
    this.refs.userEmail.getDOMNode().value=''; 

    return; 

    }, 

    render: function() { 
    var result=this.props.data; 

     return (
     <div className={result}>{result.message}</div> 

     <form className="formElem" onSubmit={this.handleSubmit}> 
      Name: <input type="text" className="userName" name="userName" ref="userName" /><br/> 
      Email: <input type="text" className="userEmail" name="userEmail" ref="userEmail" /><br/> 
      <input type="submit" value="Submit" /> 

     <form > 

     </div> 


     ); 
     } 
    }); 


    var RC= React.createClass({ 

    getInitialState: function() { 

    return {data: ""}; 
     }, 

    onFormSubmit:function(data){ 

     $.ajax({ 
      url: this.props.url, 
      dataType: 'json', 
      type: 'POST', 
      data: data, 
      success: function(data) { 

      this.setState({data: data}); 

      }.bind(this), 
      error: function(xhr, status, err) { 

      console.error(this.props.url, status, err.toString()); 

      }.bind(this) 
     }); 


    }, 
    render:function(){ 

    return <FormComp onFormSubmit={this.onFormSubmit} data={this.state.data}/> 
    } 
    }); 

    React.render(
     <RC/>, 
     document.getElementById('content') 
    ); 

risposta

8

Questo è sicuramente qualcosa che React può gestire, nessuna manipolazione DOM diretta è necessaria. Ci sei quasi, devi solo riorganizzarti un po '. Ecco un modo per avvicinarsi a questo (con i commenti in giro per importanti cambiamenti):

var FormComp = React.createClass({ 

    // To get rid of those input refs I'm moving those values 
    // and the form message into the state 
    getInitialState: function() { 
    return { 
     name: '', 
     email: '', 
     message: '' 
    }; 
    }, 

    handleSubmit: function(e) { 

    e.preventDefault(); 

    var userName = this.state.name.trim(); 
    var userEmail = this.state.email.trim(); 

    if(!userName || !userEmail) return; 

    this.setState({ 
     name: '', 
     email: '', 
     message: 'Please wait...' 
    }); 

    // I'm adding a callback to the form submit handler, so you can 
    // keep all the state changes in the component. 
    this.props.onFormSubmit({ 
     userName: userName, 
     userEmail: userEmail, 
     url: "/api/submit" 
    }, function(data) { 
     this.setState({ message: data }); 
    }); 
    }, 

    changeName: function(e) { 
    this.setState({ 
     name: e.target.value 
    }); 
    }, 

    changeEmail: function(e) { 
    this.setState({ 
     email: e.target.value 
    }); 
    }, 

    render: function() { 
    // the message and the input values are all component state now 
    return (
     <div> 
     <div className="result">{ this.state.message }</div> 
     <form className="formElem" onSubmit={ this.handleSubmit }> 
      Name: <input type="text" className="userName" name="userName" value={ this.state.name } onChange={ this.changeName } /><br /> 
      Email: <input type="text" className="userEmail" name="userEmail" value={ this.state.email } onChange={ this.changeEmail } /><br /> 
      <input type="submit" value="Submit" /> 
     </form> 
     </div> 
    ); 
    } 
}); 


var RC = React.createClass({ 

    onFormSubmit: function(data, callback){ 

    $.ajax({ 
     url: this.props.url, 
     dataType: 'json', 
     type: 'POST', 
     data: data, 
     success: callback, 
     error: function(xhr, status, err) { 

      console.error(this.props.url, status, err.toString()); 

     }.bind(this) 
     }); 
    }, 

    render: function() { 
    return <FormComp onFormSubmit={this.onFormSubmit} /> 
    } 
}); 

React.render(
    <RC />, 
    document.getElementById('content') 
); 
+0

si può avere uno sguardo a un altro Reagire domanda qui - http://stackoverflow.com/questions/27913004/react-js-render-a -componente-da-fuori-reagire? –

+0

non dovrebbe il 'userName' e' userEmail' all'interno di 'handleSubmit' dovrebbero essere calcolati da' refs' invece di 'state'? –

+0

Oops, mi dispiace. Hai dimenticato di gestire le modifiche di input, quindi lo stato è sempre aggiornato. Trovo che sia meglio memorizzare intenzionalmente quello stato, invece di archiviare in elementi dom. Ti dà più opzioni se decidi di cambiare il modo in cui funzionano le cose. – Shawn

Problemi correlati