猿问

React Native Formik 与 react redux 一起使用

我怀疑我的onSubmit函数有问题。上述错误是我在运行应用程序时遇到的。


我试图更改我的onSubmit函数以将“title”作为有效负载,但这也不起作用


协助将不胜感激。


addAuctionForm:


import React, {Component} from 'react'

import { TextInput, Button } from 'react-native-paper'

import { View } from 'react-native'

import { connect} from 'react-redux'

import { Formik } from 'formik'


import { addAuction} from '../../actions/index'



function mapDispatchToProps(dispatch) {

    return {

        addAuction: auction => dispatch(addAuction(auction))

    }

}


class ConnectedForm extends Component {

    constructor(props) {

        super(props)

        this.state = {

            title: ''

        }

        

    }

    

    render() {

        return (

            <Formik

                onSubmit={() => addAuction(auction)}

            >

                {({ handleChange, handleSubmit, values}) => (

                    <View>

                        <TextInput

                        onChangeText={handleChange('title')}

                        value={auction.title}

                        />

                        <Button onPress={handleSubmit} title="SUBMIT"/>

                    </View>     

                )}

            </Formik>

        );

    }

}


const Form = connect(null, mapDispatchToProps)(ConnectedForm)


export default Form


行动:


import { ADD_AUCTION } from '../constants/action-types'


export function addAuction(payload) {

    return { type: 'ADD_AUCTION', payload}

}

减速器:


import {ADD_AUCTION} from '../constants/action-types';


const initialState = {

    auctions: []

}


function rootReducer(state = initialState, action) {


    if (action.type === ADD_AUCTION) {

        return Object.assign({}, state, {

            auctions: state.auctions.concat(action.payload)

        })

    }

    return state

}


export default rootReducer;


慕容708150
浏览 106回答 1
1回答

江户川乱折腾

您正在使用的库 ( react-native-paper) 并且Formik不直接兼容。您将无法handle*直接使用道具。您最好的选择是直接使用setFieldValue和submitForm:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Formik&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onSubmit={() => addAuction(auction)}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {({ setFieldValue, submitForm, values}) => (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <View>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextInput&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChangeText={v => setFieldValue('title', v)}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={auction.title}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button onPress={() => submitForm()} title="SUBMIT"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </View>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Formik>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答