仅学习redux形式,并且从组件内部尝试通过代码更改字段名:
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
当我单击此“更改名字”按钮时,将执行该操作。我从redux-form导入了change操作。为什么会出现此错误:
react-dom.development.js:283 Uncaught TypeError:(0, _reactRedux.dispatch)不是函数 在changeStuff处(SelectingFormValuesForm.js:20) 在HTMLUnknownElement.callCallback(react-dom.development.js:143) 在Object.invokeGuardedCallbackDev(react-dom.development.js:193)
答案 0 :(得分:1)
更改字段值的几种方法,但是使用change
的方法如下:
import { connect, change } from 'redux-form';
change
添加到mapDispatchToProps
对象。mapDispatchToProps
中添加connect
作为第二个参数(如果不需要mapStateToProps
,则将其为connect(null, mapDispatchToProps)
。this.props.change
,props.change
和formname
呼叫fieldname
或value
。PureComponent
-易于阅读和测试。