如何以 redux 形式发送更改操作?
How to dispatch a change action in redux-form?
刚刚学习 redux-form 并在我的组件中尝试通过代码更改字段名:
Import React from 'react'
import { connect, dispatch } from 'react-redux'
import { Field, reduxForm, formValueSelector } from 'redux-form'
let SelectingFormValuesForm = props => {
const changeStuff = (props) => {
dispatch(change('selectingFormValues', 'firstName', 'Bertje'))
}
const {
favoriteColorValue,
fullName,
handleSubmit,
hasEmailValue,
pristine,
reset,
submitting,
change
} = props
return (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<div>
<Field
name="firstName"
component="input"
type="text"
placeholder="First Name"
/>
</div>
</div>
<div>
<label>Last Name</label>
<div>
<Field
name="lastName"
component="input"
type="text"
placeholder="Last Name"
/>
</div>
</div>
<div>
<label htmlFor="hasEmail">Has Email?</label>
<div>
<Field
name="hasEmail"
id="hasEmail"
component="input"
type="checkbox"
/>
</div>
</div>
{hasEmailValue && (
<div>
<label>Email</label>
<div>
<Field
name="email"
component="input"
type="email"
placeholder="Email"
/>
</div>
</div>
)}
<div>
<label>Favorite Color</label>
<div>
<Field name="favoriteColor" component="select">
<option />
<option value="#ff0000">Red</option>
<option value="#00ff00">Green</option>
<option value="#0000ff">Blue</option>
</Field>
</div>
</div>
{favoriteColorValue && (
<div
style={{
height: 80,
width: 200,
margin: '10px auto',
backgroundColor: favoriteColorValue
}}
/>
)}
<div>
<button type="submit" disabled={pristine || submitting}>
Submit {fullName}
</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>
Clear Values
</button>
<button type="button" onClick={changeStuff}>
Change first name
</button>
</div>
</form>
)
}
// The order of the decoration does not matter.
// Decorate with redux-form
SelectingFormValuesForm = reduxForm({
form: 'selectingFormValues',// a unique identifier for this form
change: reduxForm.change,
dispatch:reduxForm.dispatch
})(SelectingFormValuesForm)
// Decorate with connect to read form values
const selector = formValueSelector('selectingFormValues') // <-- same as form name
SelectingFormValuesForm = connect(state => {
//debugger
// can select values individually
const hasEmailValue = selector(state, 'hasEmail')
const favoriteColorValue = selector(state, 'favoriteColor')
// or together as a group
const { firstName, lastName } = selector(state, 'firstName', 'lastName')
return {
hasEmailValue,
favoriteColorValue,
fullName: `${firstName || ''} ${lastName || ''}`
}
})(SelectingFormValuesForm)
export default SelectingFormValuesForm
当我单击此 'change first name' 按钮时,它会被执行。我从 redux-form 导入了更改操作。为什么会出现此错误:
react-dom.development.js:283 Uncaught TypeError: (0 ,
_reactRedux.dispatch) is not a function
at changeStuff (SelectingFormValuesForm.js:20)
at HTMLUnknownElement.callCallback (react-dom.development.js:143)
at Object.invokeGuardedCallbackDev (react-dom.development.js:193)
更改字段值的几种方法,但这里是您可以使用 change
:
的方法
import { change } from 'redux-form';
- 将
change
添加到 mapDispatchToProps
对象。
- 将
mapDispatchToProps
添加到connect
中作为第二个参数(如果不需要mapStateToProps
,则为connect(null, mapDispatchToProps)
。
- 用
formname
、fieldname
和 value
调用 this.props.change
或 props.change
。
- 可选 -- 在调用辅助表单操作时使用
PureComponent
-- 更易于阅读和测试。
刚刚学习 redux-form 并在我的组件中尝试通过代码更改字段名:
Import React from 'react'
import { connect, dispatch } from 'react-redux'
import { Field, reduxForm, formValueSelector } from 'redux-form'
let SelectingFormValuesForm = props => {
const changeStuff = (props) => {
dispatch(change('selectingFormValues', 'firstName', 'Bertje'))
}
const {
favoriteColorValue,
fullName,
handleSubmit,
hasEmailValue,
pristine,
reset,
submitting,
change
} = props
return (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<div>
<Field
name="firstName"
component="input"
type="text"
placeholder="First Name"
/>
</div>
</div>
<div>
<label>Last Name</label>
<div>
<Field
name="lastName"
component="input"
type="text"
placeholder="Last Name"
/>
</div>
</div>
<div>
<label htmlFor="hasEmail">Has Email?</label>
<div>
<Field
name="hasEmail"
id="hasEmail"
component="input"
type="checkbox"
/>
</div>
</div>
{hasEmailValue && (
<div>
<label>Email</label>
<div>
<Field
name="email"
component="input"
type="email"
placeholder="Email"
/>
</div>
</div>
)}
<div>
<label>Favorite Color</label>
<div>
<Field name="favoriteColor" component="select">
<option />
<option value="#ff0000">Red</option>
<option value="#00ff00">Green</option>
<option value="#0000ff">Blue</option>
</Field>
</div>
</div>
{favoriteColorValue && (
<div
style={{
height: 80,
width: 200,
margin: '10px auto',
backgroundColor: favoriteColorValue
}}
/>
)}
<div>
<button type="submit" disabled={pristine || submitting}>
Submit {fullName}
</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>
Clear Values
</button>
<button type="button" onClick={changeStuff}>
Change first name
</button>
</div>
</form>
)
}
// The order of the decoration does not matter.
// Decorate with redux-form
SelectingFormValuesForm = reduxForm({
form: 'selectingFormValues',// a unique identifier for this form
change: reduxForm.change,
dispatch:reduxForm.dispatch
})(SelectingFormValuesForm)
// Decorate with connect to read form values
const selector = formValueSelector('selectingFormValues') // <-- same as form name
SelectingFormValuesForm = connect(state => {
//debugger
// can select values individually
const hasEmailValue = selector(state, 'hasEmail')
const favoriteColorValue = selector(state, 'favoriteColor')
// or together as a group
const { firstName, lastName } = selector(state, 'firstName', 'lastName')
return {
hasEmailValue,
favoriteColorValue,
fullName: `${firstName || ''} ${lastName || ''}`
}
})(SelectingFormValuesForm)
export default SelectingFormValuesForm
当我单击此 'change first name' 按钮时,它会被执行。我从 redux-form 导入了更改操作。为什么会出现此错误:
react-dom.development.js:283 Uncaught TypeError: (0 , _reactRedux.dispatch) is not a function at changeStuff (SelectingFormValuesForm.js:20) at HTMLUnknownElement.callCallback (react-dom.development.js:143) at Object.invokeGuardedCallbackDev (react-dom.development.js:193)
更改字段值的几种方法,但这里是您可以使用 change
:
import { change } from 'redux-form';
- 将
change
添加到mapDispatchToProps
对象。 - 将
mapDispatchToProps
添加到connect
中作为第二个参数(如果不需要mapStateToProps
,则为connect(null, mapDispatchToProps)
。 - 用
formname
、fieldname
和value
调用this.props.change
或props.change
。 - 可选 -- 在调用辅助表单操作时使用
PureComponent
-- 更易于阅读和测试。