Mik*_*tin 128 javascript crud reactjs redux
我有一个用于创建,读取,更新和删除的表单.我创建了3个具有相同形式的组件,但我传递了不同的道具.我有CreateForm.js,ViewForm.js(只读取删除按钮)和UpdateForm.js.
我曾经使用PHP,所以我总是以一种形式做这些.
我使用React和Redux来管理商店.
当我在CreateForm组件中时,我传递给我的子组件,这个props createForm={true}不会用值填充输入,也不会禁用它们.在我的ViewForm组件中,我传递了这个道具readonly="readonly".
我有一个textarea的另一个问题,它充满了一个值,并且不可更新.具有值的react textarea是readonly但需要更新
只有一个组件可以处理这些不同的表单状态,最好的结构是什么?
你有任何建议,教程,视频,演示分享?
Mik*_*tin 114
我找到了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;
Run Code Online (Sandbox Code Playgroud)
在此之后,您连接处理表单的组件:
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);
Run Code Online (Sandbox Code Playgroud)
并在您的组合减速器中添加redux-form减速器:
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;
Run Code Online (Sandbox Code Playgroud)
验证器模块看起来像这样:
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;
}
Run Code Online (Sandbox Code Playgroud)
表单完成后,如果要使用某些值填充所有字段,可以使用以下initialize函数:
componentWillMount() {
this.props.dispatch(initialize('contact', {
name: 'test'
}, ['name', 'address', 'phone']));
}
Run Code Online (Sandbox Code Playgroud)
填充表单的另一种方法是设置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);
Run Code Online (Sandbox Code Playgroud)
如果您有任何其他方式来处理这个问题,请留言!谢谢.
Ash*_*man 11
还有react-redux-form(逐步),它似乎将一些redux-form的javascript(和样板文件)与标记声明交换.它看起来不错,但我还没用过它.
来自自述文件的剪切和粘贴:
import React from 'react';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { modelReducer, formReducer } from 'react-redux-form';
import MyForm from './components/my-form-component';
const store = createStore(combineReducers({
user: modelReducer('user', { name: '' }),
userForm: formReducer('user')
}));
class App extends React.Component {
render() {
return (
<Provider store={ store }>
<MyForm />
</Provider>
);
}
}
Run Code Online (Sandbox Code Playgroud)
./components/my-form-component.js
import React from 'react';
import { connect } from 'react-redux';
import { Field, Form } from 'react-redux-form';
class MyForm extends React.Component {
handleSubmit(val) {
// Do anything you want with the form value
console.log(val);
}
render() {
let { user } = this.props;
return (
<Form model="user" onSubmit={(val) => this.handleSubmit(val)}>
<h1>Hello, { user.name }!</h1>
<Field model="user.name">
<input type="text" />
</Field>
<button>Submit!</button>
</Form>
);
}
}
export default connect(state => ({ user: state.user }))(MyForm);
Run Code Online (Sandbox Code Playgroud)
react-redux-form文档提供了与redux-form的比较:
https://davidkpiano.github.io/react-redux-form/docs/guides/compare-redux-form.html
| 归档时间: |
|
| 查看次数: |
40918 次 |
| 最近记录: |