我試圖在功能上使用此http://redux-form.com/6.6.1/examples/remoteSubmit/,這種結構:終極版表格不能做遠程提交
在index.js我宣佈關閉我的模式組件的功能(包含了Redux形式)和一個調度提交動作(用於遠程提交)的函數。兩者都通過道具傳遞給AccountScene(一個演示組件)。
在layout.js上,AccountScene組件通過props將兩個函數傳遞給CreateAccount.js(包含表單的最終組件)。在最後一個組件中,我需要在按UTModal組件上的acceptButton時提交表單(並在此測試用例中關閉模式)。
我試着將此配置參數傳遞給redux-form裝飾器:onSubmit:submitModal,但拋出「錯誤:'submitModal'未定義」。
在此先感謝。
index.js
class AccountsContainer extends Component {
state = { modalIsOpen: false };
handlePress =() => {
this.setState({ modalIsOpen: true });
};
handleModalClose =() => {
this.setState({ modalIsOpen: false });
};
submit = values => {
this.props.dispatch(submit('createaccount'));
};
render() {
return (
<AccountScene
onCreateAccount={this.handlePress}
modalIsOpen={this.state.modalIsOpen}
closeModal={this.handleModalClose}
accounts={this.props.accounts}
submitModal={this.submit}
/>
);
}
}
AccountsContainer.propTypes = {
accounts: accountPropTypes().accounts
};
const mapStateToProps = store => ({
accounts: store.account.accounts
});
export default connect(mapStateToProps)(AccountsContainer);
layout.js
function AccountScene({ onCreateAccount, modalIsOpen, closeModal, accounts, submitModal }) {
return (
<div>
<SecondaryTopbar
titleText={i18next.t('accounts:accounts')}
titleIcon={icon}
iconAltText={i18next.t('accounts:accounts')}
>
<UTButton base onPress={onCreateAccount} green type="submit">
{i18next.t('accounts:addAccount')}
</UTButton>
</SecondaryTopbar>
<AccountList accounts={accounts} />
<CreateAccount modalIsOpen={modalIsOpen} closeModal={closeModal} submitModal={submitModal} onSubmit={closeModal} />
</div>
);
}
AccountScene.propTypes = {
onCreateAccount: PropTypes.func,
modalIsOpen: PropTypes.bool.isRequired,
closeModal: PropTypes.func.isRequired,
accounts: accountPropTypes().accounts
};
export default reduxForm({ form: 'createaccount' })(AccountScene);
CreateAccount.js
let CreateAccount = props => {
const {closeModal, modalIsOpen, submitModal, handleSubmit} = props
return (
<div>
<UTModal
isOpen={modalIsOpen}
onRequestClose={() => this.setState({ modalIsOpen: false })}
acceptButton={{ text: 'Add', onPress:() => "I need to submit form here!" }}
title={'Create account'}
>
<UTLabel black>Some text</UTLabel>
<div>
<form onSubmit={handleSubmit} noValidate>
<Field
component={UTFormInput}
name="accountNumber"
type="text"
placeholder="Customer Number"
validate={[InputValidations.required('Required')]}
/>
</form>
</div>
</UTModal>
</div>
);
}
CreateAccount.propTypes = {
modalIsOpen: PropTypes.bool.isRequired,
closeModal: PropTypes.func.isRequired
};
export default reduxForm({ form: 'createaccount', onSubmit: submitModal })(CreateAccount);
好的,這解釋了錯誤信息,但我可以做的是,當我按父母UTModal組件上的acceptButton時,表單被提交? –
我確定你可以。我將編輯我的答案,並提供我認爲可行的代碼 –
我試過這個,但是這不起作用,現在拋出: 未捕獲(承諾)TypeError:無法讀取屬性'的道具'未定義 在CreateAccount (bundle.js:82241) –