我正在尝试使用"redux-form":"^6.7.0“和"react-bootstrap":"^0.31.0”。
我的组件呈现得很好,但是当我按Submit时,我看到的是一个空对象。
注意:我尝试过先用connect包装Config,然后如下面所示,首先用redux-form包装它,然后用from react redux connect()包装它。
Configuration.js
class Config extends Component {
render() {
const { ServerPort, UserID, PortNumber, WWWUrl, SourcePath, FMEPath, pickFile, pickFolder, handleSubmit } = this.props;
return (
<Form horizontal onSubmit={handleSubmit}>
<FormGroup controlId="serverPortBox">
<Col componentClass={ControlLabel} sm={2}>Watson Port:</Col>
<Col sm={10}>
<OverlayTrigger placement="left" overlay={(<Tooltip id="tt1">TCP port for Watson to use</Tooltip>)}>
<Field name="WatsonPort" component={FormControl}
type="number" min="1024" max="65535" placeholder={ServerPort} />
</OverlayTrigger>
</Col>
</FormGroup>
......
const CForm = reduxForm({
form: 'configuration' // a unique name for this form
})(Config);
const Configuration = connect(mapStateToProps, mapDispatchToProps)(CForm)
export default Configurationreducers.js
import { combineReducers } from 'redux'
import { reducer as formReducer } from 'redux-form
......
const reducerList = {
GLVersion,
ServerConfig,
ServerStats,
form: formReducer
}
export default combineReducers(reducerList)主包Dashboard.js
我在调试器中看到的是config是一个空对象。
<Panel className='configPanel'
collapsible header="Configuration"
eventKey="1"
defaultExpanded={true}>
<Configuration onSubmit={(config) => writeConfig(config)} />
</Panel>发布于 2017-05-14 17:35:54
请参阅:https://github.com/erikras/redux-form/issues/2917
哦,这是个很大的谜。我遵循了https://github.com/react-bootstrap/react-bootstrap/issues/2210中的建议,停止了关于附加道具和空提交的警告。
似乎您必须在您的自定义组件中包装引导带(为什么?,我不知道)。另外,请确保您的自定义组件是一个无状态函数组件,或者在第一个键按下之后,您的字段将变得模糊并失去焦点。
redux表单的文档中有一些关于这方面的警告。
我的自定义字段组件FieldInput
const FieldInput = ({ input, meta, type, placeholder, min, max }) => {
return (
<FormControl
type={type}
placeholder={placeholder}
min={min}
max={max}
value={input.value}
onChange={input.onChange} />
)
}我是这样引用的:
<Field name="ServerPort"
type='number'
component={FieldInput}
placeholder={ServerPort}
min="1024" max="65535"
/>另见:https://github.com/erikras/redux-form/issues/1750
现在,FieldInput和Config的定义如下所示:
import React, { Component } from 'react'
import { Field, reduxForm } from 'redux-form'
import { connect } from 'react-redux'
import { Form, FormControl, FormGroup, ControlLabel, Col, Button, Tooltip, OverlayTrigger } from 'react-bootstrap'
import * as Act from '../dash/actions.js'
import FaFolderOpen from 'react-icons/lib/fa/folder-open'
import FaFileCodeO from 'react-icons/lib/fa/file-code-o'
const FieldInput = ({ input, meta, type, placeholder, min, max }) => {
return (
<FormControl
type={type}
placeholder={placeholder}
min={min}
max={max}
value={input.value}
onChange={input.onChange} />
)
}
const Config = ({ ServerPort, UserID, PortNumber, WWWUrl, SourcePath, FMEPath, pickFile, pickFolder, handleSubmit }) => {
return (
<Form horizontal onSubmit={handleSubmit}>
<FormGroup controlId="serverPortBox">
<Col componentClass={ControlLabel} sm={2}>Watson Port:</Col>
<Col sm={10}>
<OverlayTrigger placement="left" overlay={(<Tooltip id="tt1">TCP port for Watson to use</Tooltip>)}>
<Field name="ServerPort" type='number' min="1024" max="65535" component={FieldInput} placeholder={ServerPort} />
</OverlayTrigger>
</Col>
</FormGroup>发布于 2017-07-17 22:16:46
<FormControl>所需的一些道具是从<Field>在props.input内部传递的,参见http://redux-form.com/6.6.3/docs/api/Field.md/#props
要以通用的方式传递所有这些道具,您可以使用以下函数,而不是显式地这样做:
const ReduxFormControl = ({input, meta, ...props}) => {
return <FormControl {...props} {...input} />
};然后在表格里:
<Field component={ReduxFormControl} ... />这样,值、onChange等都会按预期传递给<FormControl>。
https://stackoverflow.com/questions/43924077
复制相似问题