【发布时间】:2017-01-10 01:11:41
【问题描述】:
这是我第一次使用 React Native,只是短暂地使用过 Redux,但我的 Redux/authentication.js 中有一个函数应该可以使用 Firebase 创建一个新帐户。
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)
当我console.log(this.props) 时,它告诉我dispatch 可以作为道具使用
但是当我尝试在 handleSignUp 方法中执行 this.props.dispatch(handleAuthWithFirebase(this.state)) 时,我收到一个错误,即 props 是 undefined
【问题讨论】:
标签: javascript react-native redux react-redux