首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法显示表单表单

无法显示表单表单
EN

Stack Overflow用户
提问于 2020-07-23 02:37:40
回答 1查看 23关注 0票数 0

我正在尝试显示我的formik表单,这是我第一次使用它。然而,屏幕是完全空白的。我想这与我的造型有关,但我不确定。下面是我的代码:

代码语言:javascript
复制
export default class Checkout extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Formik
          initialValues={{first_name: '', last_name: ''}}
          onSubmit={(values) => {
            console.log(values);
          }}>
          {(props) => {
            <View>
              <TextInput
                style={styles.input}
                placeholder="first name"
                onChangeText={props.handleChange('first_name')}
                value={props.values.first_name}
              />
              <TextInput
                style={styles.input}
                placeholder="last name"
                onChangeText={props.handleChange('last_name')}
                value={props.values.last_name}
              />
              <Button
                title="place order"
                color="maroon"
                onPress={props.handleSubmit}
              />
            </View>;
          }}
        </Formik>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  input: {
    borderWidth: 1,
    borderColor: 'black',
    padding: 10,
    fontSize: 18,
    borderRadius: 6,
  },
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-23 02:55:03

由于您使用的是{},因此应该返回类似以下内容的内容

代码语言:javascript
复制
<Formik
          initialValues={{first_name: '', last_name: ''}}
          onSubmit={(values) => {
            console.log(values);
          }}>
          {(props) => {return (<View ...} }

或者你可以在Formik中删除这些{},然后不需要输入return语句,因为你只返回一个东西。下面是你应该做的

代码语言:javascript
复制
export default class Checkout extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Formik
          initialValues={{first_name: '', last_name: ''}}
          onSubmit={(values) => {
            console.log(values);
          }}>
          {(props) => 
            <View>
              <TextInput
                style={styles.input}
                placeholder="first name"
                onChangeText={props.handleChange('first_name')}
                value={props.values.first_name}
              />
              <TextInput
                style={styles.input}
                placeholder="last name"
                onChangeText={props.handleChange('last_name')}
                value={props.values.last_name}
              />
              <Button
                title="place order"
                color="maroon"
                onPress={props.handleSubmit}
              />
            </View>;
          }
        </Formik>
      </View>
    );
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63041038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档