简体   繁体   English

React Native 中的 HandleSubmit Redux 表单不起作用(无值打印出来)

[英]HandleSubmit Redux Form in React Native doesn't work (No Values Print Out)

I am using Redux Form to capture user info submission.我正在使用 Redux 表单来捕获用户信息提交。 I've connected Redux Form with the store and written the form according to the instructions, but when clicking the submit button, no values are passed through.我已经将 Redux Form 与 store 连接起来,并根据说明编写了表单,但是当单击提交按钮时,没有传递任何值。

I do not usually ask on Stack Overflow so please excuse for my bad wording.我通常不问 Stack Overflow,所以请原谅我的措辞不好。 I do not know how to articulate my problem.我不知道如何阐明我的问题。

I copied my code on to snack expo (link: https://snack.expo.io/S1_6f7dQV )我将我的代码复制到小吃博览会(链接: https : //snack.expo.io/S1_6f7dQV

Here are my codes: Components/Form.js这是我的代码: Components/Form.js

import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form';
import { StyleSheet, View, Text, TouchableOpacity, TextInput } from 'react-native';

const renderField = ({ label, keyboardType, name }) => {
    return (
      <View>
        <Text>{label}</Text>
        <TextInput  keyboardType={keyboardType}
        >
        </TextInput>
    </View>
    );
};

const submit = values => { 
    alert(`here is the value ${JSON.stringify(values)}`)
}
const ContactComponent = props => {
    const { handleSubmit } = props;
    console.log('handle submit ...');
    console.log(handleSubmit);
    return (
        <View>
            <Text>Redux-form example</Text>
            <Field keyboardType="default" label="Username: " component={renderField} name="Username" />
            <Field keyboardType="email-address" label="Email: " component={renderField} name="Email" />
            <TouchableOpacity onPress={handleSubmit(submit)} style={{ margin: 10, alignItems: 'center' }}>
                <Text>Submit</Text>
            </TouchableOpacity>
        </View>
    );
}

const ContactForm = reduxForm({
    form: 'contact', // a unique identifier for this form
})(ContactComponent);

export default ContactForm;

Component/MainComponent.js组件/MainComponent.js

import { createStackNavigator, createAppContainer } from 'react-navigation';
import HomeScreen from '../screens/HomeScreen';
import React, { Component } from 'react';
import { View, Icon} from 'native-base';

const MainNavigator = createStackNavigator({
  Home: { screen: HomeScreen }
  // AddTransaction: { screen: AddTransaction },
  // TransactionList: { screen: TransactionList }
})

const Main = createAppContainer(MainNavigator);
export default Main;

Screen/HomeScreen.js屏幕/HomeScreen.js

import React, {Component} from 'react';
import { Container, View, Text, Content, Button, Form } from 'native-base';
import ContactForm from '../components/Form.js';

class HomeScreen extends Component {

  static navigationOptions = { 
    title: 'Home',
  }

  render() {   
    return (
      <Container>
          <ContactForm/>
      </Container>
    );
  }
}

export default HomeScreen;

App.js应用程序.js

import React from 'react';
import { View, Text } from 'native-base';
import Main from './components/MainComponent';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { createLogger } from 'redux-logger';
import { reducer as formReducer } from 'redux-form';

const rootReducer = combineReducers({
  form: formReducer, 
});
export const store = createStore(rootReducer)

export default class App extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <Main />
      </Provider>
    );
  }
}

截屏

Try including the input props to yout TextInput like below, as shown in the example here on ReduxForm's docs尝试将input道具包含到您的TextInput中,如下所示,如ReduxForm 文档中的示例所示

const renderField = ({ label, keyboardType, name, input }) => {
    return (
      <View>
        <Text>{label}</Text>
        <TextInput  keyboardType={keyboardType} {...input}
        >
        </TextInput>
    </View>
    );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM