我目前有一個非常簡單的React應用程序,它使用狀態來確定幾個視圖。點擊submit元素可以簡單地調用一個傳遞登錄或註冊憑證的端點。我可以沿着這條路繼續使用從服務器返回的React和JSON來確定我的視圖,但我知道這不是處理數據的方式。瞭解如何使用Flummox實現Flux體系結構
我的問題是,從這裏,我該如何使用Flummox實現Flux模式?我是否會對有效載荷進行操作並在操作或商店中進行API調用?我試圖理解從UI中的動作到我的API調用,以便獲取響應和UI對商店中的更改做出響應的正確方法。
以下是我的Auth組件的代碼。我想看一個如何在這種簡單的情況下使用Flummox的例子,所以我可以理解需要擴展和添加功能。
Auth.jsx
'use strict';
var React = require('react');
var request = require('request');
var Auth = React.createClass({
getInitialState: function() {
return {
name: '',
email: '',
pass: '',
passConfirm: '',
login: true,
register: false
};
},
handleLogin: function(){
this.setState({
login: true,
register: false
});
},
handleRegister: function(){
this.setState({
register: true,
login: false
});
},
handleName: function(event){
this.setState({
name: event.target.value
});
},
handleEmail: function(event){
this.setState({
email: event.target.value
});
},
handlePass: function(event){
this.setState({
pass: event.target.value
});
},
handlePassConfirm: function(event){
this.setState({
passConfirm: event.target.value
});
},
handleSubmit: function(){
var register = {
name: this.state.name,
email: this.state.email,
password: this.state.pass,
confirmPassword: this.state.passConfirm
};
var endpoint = 'http://localhost:3000';
request({
uri: endpoint + '/register',
method: 'POST',
json: register
}, function(error, response, body){
if (error) {
console.log(error);
} else {
console.log(response);
}
});
},
renderLogin: function() {
return (
<form className="login">
<div className="input-container">
<input type='text' value={this.state.email} onChange={this.handleEmail} placeholder="email" />
<input type='password' value={this.state.password} onChange={this.handlePass} placeholder="password" />
</div>
<div className="auth-submit" onClick={this.handleSubmit}>Login</div>
</form>
);
},
renderRegister: function(){
return (
<form className="register">
<div className="input-container">
<input type='text' value={this.state.name} onChange={this.handleName} placeholder="name" />
<input type='email' value={this.state.email} onChange={this.handleEmail} placeholder="email" />
<input type='password' value={this.state.pass} onChange={this.handlePass} placeholder="password" />
<input type='password' value={this.state.passConfirm} onChange={this.handlePassConfirm} placeholder="confirm password" />
</div>
<div className="auth-submit" onClick={this.handleSubmit}>Register</div>
</form>
);
},
render: function(){
var auth = null;
if (this.state.login) {
auth = this.renderLogin();
} else if (this.state.register) {
auth = this.renderRegister();
}
return (
<div className="auth-container">
<div className="auth-logo">Flow</div>
{auth}
<div className="auth-select">
<div className="auth-label-container">
<div className="auth-label" onClick={this.handleLogin}>Login</div>
</div>
<div className="auth-label-container">
<div className="auth-label" onClick={this.handleRegister}>Register</div>
</div>
</div>
</div>
);
},
});
module.exports = Auth;
這正是我正在尋找的解釋類型。感謝您抽出時間詳細說明這一過程,並提出如何重構該簡單組件的冗長性。 –