這是我第一次使用React Native,並且只使用Redux,但我的Redux/authentication.js
中有一個函數,該函數應該使用Firebase創建一個新帳戶。在React Native Redux中調度導入的函數
export function handleAuthWithFirebase (newUser) {
return function (dispatch, getState) {
dispatch(authenticating());
console.log(newUser);
console.log('Signing up user');
var email = newUser.email;
var password = newUser.password;
firebase.auth().createUserWithEmailAndPassword(email, password).catch(error => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
}).then(() => {
var user = firebaseAuth.currentUser;
// Set user in Firebase
firebase.database().ref('/users/' + user.uid).set({
displayName: newUser.displayName,
userName: newUser.userName,
email: newUser.email
})
})
dispatch(isAuthed(user.uid))
}
}
我導入此功能爲SignUpForm組件,獲取用戶信息,對一些<TextInput/>
的。所以現在我想運行handleSignUp
函數,但我不完全確定如何。
class SignUpForm extends Component {
static propTypes = {
}
state = {
email: '',
password: '',
username: '',
displayName: ''
}
handleSignUp() {
// Want to call handleAuthWithFirebase(this.state) here.
}
render() {
console.log(this.props);
return (
<View style={{flex: 1}}>
<View>
<TextInput
style={styles.input}
onChangeText={(email) => this.setState({email})}
value={this.state.email}
autoCorrect={false}
/>
<TextInput
style={styles.input}
onChangeText={(password) => this.setState({password})}
value={this.state.password}
autoCorrect={false}
/>
<TextInput
style={styles.input}
onChangeText={(username) => this.setState({username})}
value={this.state.username}
autoCorrect={false}
/>
<TextInput
style={styles.input}
onChangeText={(displayName) => this.setState({displayName})}
value={this.state.displayName}
autoCorrect={false}
/>
</View>
<View>
<Button title="Sign Up" onPress={this.handleSignUp}>Sign Up</Button>
</View>
</View>
)
}
}
export default connect()(SignUpForm)
它顯示我dispatch
可作爲道具,當我console.log(this.props)
,但是當我嘗試在handleSignUp
方法做this.props.dispatch(handleAuthWithFirebase(this.state))
我得到道具的錯誤undefined