Javascript 自定义输入-重复表单
我有以下CustomInput组件:Javascript 自定义输入-重复表单,javascript,reactjs,redux,redux-form,Javascript,Reactjs,Redux,Redux Form,我有以下CustomInput组件: import React from 'react'; const CustomInput = props => ( <div className="form-group"> <label className="form-label">{props.title}</label> <input className="form-input" name={props.nam
import React from 'react';
const CustomInput = props => (
<div className="form-group">
<label className="form-label">{props.title}</label>
<input
className="form-input"
name={props.name}
type={props.inputType}
value={props.content}
onChange={props.controlFunc}
placeholder={props.placeholder}
/>
</div>
);
CustomInput.propTypes = {
inputType: React.PropTypes.oneOf(['text', 'number']).isRequired,
title: React.PropTypes.string.isRequired,
name: React.PropTypes.string.isRequired,
controlFunc: React.PropTypes.func.isRequired,
content: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]).isRequired,
placeholder: React.PropTypes.string,
};
export default CustomInput;
其次,如果我将inputType设置为text,它将呈现:
[object Object]
因此,控制台发出以下警告:
Warning: Failed prop type: Invalid prop `content` supplied to `CustomInput`.
如何正确设置内容?我认为问题在于您正在尝试将字符串作为对象传递
<CustomInput
inputType="number"
title="How many items do you currently own?"
name="currentItems"
controlFunc={param => field.input.onChange(param.val)}
content={field.input.value}
placeholder="Number of items"
/>
您通过道具传递对象,必须传递字符串或数字
content={{ val: field.input.value }} // no!
content={field.input.value} // yes! :)
是 啊它解决了警告和问题。但是,现在我无法更改输入中的值,你知道为什么吗?我是第一个::DD@FacundoGFlores受控输入是您的单词:。当通过props对象将值传递给input时,必须为input提供onChange方法以更改传递的对象。解决方案1:将值重命名为defaultValue,解决方案2:阅读有关受控输入的内容:谢谢,但问题在于内容。我可以通过其他道具发送对象,但我只需要传递field.input.value。我正在尝试使用此自定义组件进行redux表单,但我不清楚如何传递content和controlFunc,即现在我更改了发送到content的道具,警告得到解决,但我无法更改输入的值:可能需要以其他方式传递参数我注意到的第一件事是字符串,我关注它们,但是的,content属性似乎接受的不是对象,而是字符串或数字:祝您进一步编码好运!{param=>field.input.onChangeparam.val}这对我不起作用,但{param=>field.input.onChangeparam.target.value}运行正常。也许是因为新版本?
<CustomInput
inputType="number"
title="How many items do you currently own?"
name="currentItems"
controlFunc={param => field.input.onChange(param.val)}
content={field.input.value}
placeholder="Number of items"
/>
content={{ val: field.input.value }} // no!
content={field.input.value} // yes! :)