我正在使用反应-还原形式,然后按照快速启动创建了react组件:
该类包含存储定义和提供程序。
import React from 'react';
import { Provider } from 'react-redux';
import { createStore,applyMiddleware } from 'redux';
import { combineForms } from 'react-redux-form';
import RecordForm from './components/RecordForm.jsx'
const initRecord= {
name: '',
SUKL: '',
ATC: ''
};
const store = createStore(combineForms({
record: initRecord
}));
@translate(['record'], { wait: true })
export default class App extends React.Component {
constructor(props){
super(props);
}
render() {
const { t }= this.props;
return (
<div>
<div className="row">
<div className="row header">
<h1>
{t('record.NewRecord')}
</h1>
</div>
<div className="row">
<Provider store={ store }>
<RecordForm />
</Provider>
</div>
</div>
</div>
);
}这是表单文件:
import React from 'react';
import { connect } from 'react-redux';
import { Control, Form } from 'react-redux-form';
export default class RecordForm extends React.Component {
constructor(props){
super(props);
}
handleSubmit(record) {
const { dispatch } = this.props;
///dispatch is undefined !!!!!!!
}
render() {
return (
<div>
<Form model="record" onSubmit={(record) => this.handleSubmit(record)}>
<Control.text model="record.name" />
<button type="submit">
OK!
</button>
</Form>
</div>
);
}
}当我到达handleSumbit部件-分派是未定义的。当我调试它时,即使在RecordForm的构造器中,也有n个o调度在道具或任何与形成相关的东西中。我应该添加像connect()这样的注释吗?我错过了什么?
发布于 2017-02-04 16:02:23
您需要使用connect()函数连接组件。正如在redux文档中提到的那样,您可以只使用没有任何参数的connect()。
引用提供的链接:
注入只是发送,而不听存储 导出默认连接()(TodoApp)
但是,如果您提供自定义的mapDispatchToProps函数作为参数,则不会向您提供分派。如果您仍然希望它可以作为道具使用,则需要在mapDispatchToProps实现中自己显式地返回它。你可以在常见问题科的redux上读到它。
此外,如果您想尝试实验性的decorator特性,可以使用babel。在这种情况下,您可以使用@connect连接组件,如下所示。
@connect()
export default class MyComponent extends React.Component {
// ... your code goes here.
} https://stackoverflow.com/questions/42042268
复制相似问题