栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

React + Redux-在表单组件中处理CRUD的最佳方法是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

React + Redux-在表单组件中处理CRUD的最佳方法是什么?

我找到了Redux Form包。做得很好!

因此,您可以将Redux与React-
Redux一起使用

首先,您必须创建一个表单组件(显然):

import React from 'react';import { reduxForm } from 'redux-form';import validateContact from '../utils/validateContact';class ContactForm extends React.Component {  render() {    const { fields: {name, address, phone}, handleSubmit } = this.props;    return (      <form onSubmit={handleSubmit}>        <label>Name</label>        <input type="text" {...name}/>        {name.error && name.touched && <div>{name.error}</div>}        <label>Address</label>        <input type="text" {...address} />        {address.error && address.touched && <div>{address.error}</div>}        <label>Phone</label>        <input type="text" {...phone}/>        {phone.error && phone.touched && <div>{phone.error}</div>}        <button onClick={handleSubmit}>Submit</button>      </form>    );  }}ContactForm = reduxForm({  form: 'contact',// the name of your form and the key to       // where your form's state will be mounted  fields: ['name', 'address', 'phone'], // a list of all your fields in your form  validate: validateContact  // a synchronous validation function})(ContactForm);export default ContactForm;

之后,您将连接处理该表单的组件:

import React from 'react';import { connect } from 'react-redux';import { initialize } from 'redux-form';import ContactForm from './ContactForm.react';class App extends React.Component {  handleSubmit(data) {    console.log('Submission received!', data);    this.props.dispatch(initialize('contact', {})); // clear form  }  render() {    return (      <div id="app">        <h1>App</h1>        <ContactForm onSubmit={this.handleSubmit.bind(this)}/>      </div>    );  }}export default connect()(App);

并在合并的reduce中添加redux形式reduce:

import { combineReducers } from 'redux';import { appReducer } from './app-reducers';import { reducer as formReducer } from 'redux-form';let reducers = combineReducers({  appReducer, form: formReducer // this is the form reducer});export default reducers;

验证器模块如下所示:

export default function validateContact(data, props) {  const errors = {};  if(!data.name) {    errors.name = 'Required';  }  if(data.address && data.address.length > 50) {    errors.address = 'Must be fewer than 50 characters';  }  if(!data.phone) {    errors.phone = 'Required';  } else if(!/d{3}-d{3}-d{4}/.test(data.phone)) {    errors.phone = 'Phone must match the form "999-999-9999"'  }  return errors;}

表单完成后,当您想用一些值填充所有字段时,可以使用以下

initialize
函数:

componentWillMount() {  this.props.dispatch(initialize('contact', {    name: 'test'  }, ['name', 'address', 'phone']));}

填充表单的另一种方法是设置initialValues。

ContactForm = reduxForm({  form: 'contact',// the name of your form and the key to  fields: ['name', 'address', 'phone'], // a list of all your fields in your form  validate: validateContact  // a synchronous validation function}, state => ({  initialValues: {    name: state.user.name,    address: state.user.address,    phone: state.user.phone,  },}))(ContactForm);

如果您还有其他解决方法,请留言!谢谢。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/448896.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号