2 回答
TA贡献1818条经验 获得超8个赞
我不熟悉 FormContext/Enhancer,但是我认为您的错误在于您的增强器。您正在解构从 onSubmit 处理程序返回的对象,以寻找属性“stateKostul”。“stateKostul”可能未在 FormContainer 的状态中定义。这只是您传递给它的变量的名称。
尝试改变:
handleSubmit: ({stateKostul}) => () => {
console.log('it works!');
console.log(stateKostul);
//fetch to server
}
至:
handleSubmit: (stateKostul) => () => {
console.log('it works!');
console.log(stateKostul);
//fetch to server
}
TA贡献1780条经验 获得超4个赞
我将 AddProductForm 从功能组件更改为类组件并添加了方法 handleSubmit。猜猜问题出在上下文上。不确定如何,但现在有效
这是我的代码:
class AddProductForm extends React.Component{
constructor(props){
super(props);
this.state = {
title: '',
location: '',
description: '',
photos: [],
price: '',
};
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(stateKostul) {
console.log('it works!');
console.log(stateKostul);
//fetch to server
}
render() {
return (
<div className={s.container}>
<h2 className={s.formTitle}>Add product</h2>
<div className={s.formContainer}>
<FormContainer initialValue={this.state} handleSubmit={this.handleSubmit}>
// custom inputs and submit button
</FormContainer>
</div>
</div>
);
}
}
添加回答
举报