私はマップ関数を使用しており、マップ関数内でこれが必要に応じてクラスオブジェクトを参照していることをテストして確認しました。これをマップ内の関数にバインドする
これをjsx関数にバインドして再度渡すと、必要に応じて渡されません。
エラー:キャッチされない例外TypeError:。_this3.checkType(...)バインドが機能
// wrapped in a class
checkType(type, options, placeholder, name, handleUpdatedValue, defvalue, index) {
console.log(this);
switch(type) {
case 'select':
return <select onChange={handleUpdatedValue.bind(this)} >{options.map((option, i) => <option value={option} key={i}>{option}</option>)}</select>
break;
case 'text':
return <input onChange={handleUpdatedValue.bind(this)} name={name} placeholder={placeholder} type="text" />
break;
case 'date':
return <input onChange={handleUpdatedValue.bind(this)} name={name} placeholder={placeholder} type="date" />
break;
default:
console.log('Error: Invalid Type');
}
return type === 'select' ? <select></select> : <input />
}
return(
<div className="formwrapper thinshadow">
<h3>{this.props.header}</h3>
{this.getFields().map((field, i) => {
<div key={i} className={field.classes}>
{this.checkType(field.type, field.options, field.placeholder, field.name, this.handleUpdatedValue.bind(this), field.defvalue, field.index).bind(this)}
// DOES NOT WORK^
<div className="minilabel"></div>
</div>
}, this)} // THIS WORKS
<button className="btn btn-primary"
onClick={() => this.props.onClick(values)} >
Save
</button>
</div>
);
私はこれを維持しながら呼び出しようとしています。関数の目的は、正しいフィールドを返すことです。 .bindを実行すると、エラーは取り除かれますが、関数は呼び出されません。私はあなたの解決策を理解していない場合、私はこれで新しいですので、私をよろこんでください。心配しないでください、ちょうどあなたのリンクを見ました、ありがとう! – Sequential
@ Sequential単にthis.checkType(...)を呼び出すことができます。私は詳細を編集するために追加します。 –
パーフェクト@Edmund Leeありがとう – Sequential