tom*_*tom 5 javascript reactjs react-native native-base
我在这里问这个问题感到很疯狂,但是我找不到任何有关如何提交表单和捕获RN数据的好的教程。我发现的所有东西都是有人在推动一个库“只是npm install react-native-form-genie-magic-box并在您的项目中调用它 ”。
但是我只想知道-如何在香草React Native中提交表单。
示例代码:
AuthContainer
class AuthContainer extends Component {
render() {
const { errorMessage, handleLogin } = this.props
return (
<Login
errorMessage={errorMessage}
onLoginClick={(e) => handleLogin(e)}
/>
)
}
}
.....
const mapDispatchToProps = (dispatch) => {
return {
handleLogin: (e) => {
e.preventDefault()
const form = e.target
const data = serialize(form, {hash: true})
const creds = { email:data.email, password: data.password }
dispatch(loginUser(creds))
},
}
}
Run Code Online (Sandbox Code Playgroud)
登录
import { Container, Content, Form, Item, Input, Label, Button, Text } from 'native-base';
....
const Login = ({errorMessage, onLoginClick}) => {
return (
<Container>
<Content>
<Form >
{errorMessage &&
<Text>{errorMessage}</Text>
}
<Item floatingLabel>
<Label>Email</Label>
<Input
type="email"
name="email"
/>
</Item>
<Item floatingLabel last>
<Label>Password</Label>
<Input secureTextEntry={true} />
</Item>
<Button onPress={onLoginClick} ><Text>Sign in</Text></Button>
</Form>
</Content>
</Container>
)
}
Run Code Online (Sandbox Code Playgroud)
问题: 如何仅在AuthContainer的handleLogin功能中捕获提交的电子邮件和密码?
<input您需要添加类似这样的内容,例如:
<Input onChangeText={(text) => this.setState({username: text})} value={this.state.username}
Run Code Online (Sandbox Code Playgroud)
当你使用该onPress函数时,你只需要获取 this.state.username 并在需要时使用它。我通常不会执行处理Login或其他函数的函数.js,因此您需要将 传递this.state.username给page处理它的函数。
如果我确实需要将某些东西传递给其他人,我通常page会使用GLOBALS,例如:
// globals.js
module.exports = {
username: '',
};
Run Code Online (Sandbox Code Playgroud)
然后使用globals.js
// import the globals.js
GLOBAL = require('./globals');
<Input onChangeText={(text) => this_onButtonPressed(text) value={this.state.username}/>
_onButtonPressed(text){
GLOBAL.username = this.state.username
// call function that handles it
}
Run Code Online (Sandbox Code Playgroud)
然后在page处理它时,您需要import再次使用 GLOBAL.username。
如果您不明白,请告诉我,我会尽力更好地解释它,我需要知道您是否想login在不同的地方处理它.js,或者可以在.js有表格的地方处理(这样更容易)
| 归档时间: |
|
| 查看次数: |
12047 次 |
| 最近记录: |