私は、次のCustomInputコンポーネントがあります。カスタム入力 - Reduxのフォーム
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;
をそして、これは私のフォームです:私は私のコンポーネントをレンダリングすることができ
import React, { PropTypes } from 'react';
import { Field, reduxForm } from 'redux-form';
import CustomInput from '../components/CustomInput';
const renderMyStrangeInput = field => (
<CustomInput
inputType={'number'}
title={'How many items do you currently own?'}
name={'currentItems'}
controlFunc={param => field.input.onChange(param.val)}
content={{ val: field.input.value }}
placeholder={'Number of items'}
/>
);
class ItemsForm extends React.Component {
constructor(props) {
super(props);
}
render() {
const { handleSubmit } = this.props;
return (
<form className="container" onSubmit={handleSubmit}>
<h1>Nuevo Pedido</h1>
<Field name="myField" component={renderMyStrangeInput} />
<div className="form-group">
<button type="submit" className="btn btn-primary input-group-btn">Submit</button>
</div>
</form>
);
}
}
ItemsForm.propTypes = {
};
ItemsForm = reduxForm({
form: 'Items',
})(ItemsForm);
export default ItemsForm;
が、コンテンツ・タイプを持ついくつかの問題があります。私はCustomInput
を設定した場合、まず、数字を受け入れるために私が手:
he specified value "[object Object]" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?
第二に、私はテキストにinputType
を設定した場合、それはレンダリング:
[object Object]
をので、コンソールは次の警告を与えています:
Warning: Failed prop type: Invalid prop `content` supplied to `CustomInput`.
コンテンツを正しく設定するにはどうすればよいですか。
ありがとうございました。問題はコンテンツにありました。私はオブジェクトの残りの部分を介して送信することができますが、私は 'field.input.value'だけを渡す必要がありました。私はこのカスタムコンポーネントを 'redux-form'のために使いこなそうとしていますが、' content'と 'controlFunc'をどのように渡すべきかはわかりません。つまり、今度はpropを内容に変更して警告を解決しました。しかし、私は入力の値を変更することはできません:(多分paramは別の方法で渡す必要があります – FacundoGFlores
私が気づいた最初のことはStringsと私はそれらに焦点を当てましたが、はいまたcontentプロパティはオブジェクトではなくStringまたはNumberを受け入れるようです: ))さらに幸運とコーディング! – Kejt