![](/img/trans.png)
[英]React functional component TypeError: Cannot read property 'props' of undefined
[英]How to get input value in functional component in react.js? (TypeError: Cannot read property 'target' of undefined)
我正在嘗試將數據添加到 Cloud Firestore,但在單擊按鈕時出現此錯誤
TypeError: Cannot read property 'target' of undefined
這是代碼。
function Note(props) {
function ReplyAnswer(e)
{
var date = moment().utcOffset('+05:30').format('hh:mm A DD-MM-YYYY');
firebase.firestore().collection('qna2dreply').add(
{
rcontent: e.target.value,
replyby: userName,
replybyid: userID,
replytoid: props.id,
time: new Date(),
time2: date
})
.then(() => {
document.getElementById('xxx').value = '';
swal("Successfully Replied to "+props.title);
});
}
return (
<div>
<div className="note">
<h1>{props.title} <span className="time"> {(props.time).toString()}</span></h1>
<p>{props.content}
<input
className="replyForminput"
name="content"
required
placeholder={'Reply to '+props.title}
autoCorrect="off"
autoComplete="off" />
<button onClick={() => { ReplyAnswer() }}
className="replyBtn"> Reply </button></p>
{props.id == idx ? (<div><RBox /></div>) : null}
</div>
</div>
);
}
請幫忙
..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ……
首先 - 對於e
undefined
:
您應該只將ReplyAnswer
傳遞給button
的onClick
事件,不要將其包裝在箭頭 function 中:
<button onClick={ReplyAnswer} className="replyBtn"> Reply </button>
這會將事件ReplyAnswer
作為e
參數傳遞給您的 ReplyAnswer。 如果您將ReplyAnswer
包裝在箭頭 function 中,並在不向其傳遞任何參數的情況下調用它,就像您所做的那樣:
() => { ReplyAnswer() }
您可以看到ReplyAnswer
沒有收到任何參數,因此您的e
是undefined
。 你的箭頭 function 也是一個零參數 function,所以它不會收到來自onClick
事件 object 的事件。
第二 - 對於e.target.value
是null
:
因為您在button
的onClick
事件中調用ReplyAnswer
,所以e.target.value
是null
。
為了解決這個問題,您可以將input
和button
包裝在一個form
中,如下所示:
<form onSubmit={ReplyAnswer}>
<input
className="replyForminput"
name="content"
required
placeholder={'Reply to '+props.title}
autoCorrect="off"
autoComplete="off" />
<button type="submit" className="replyBtn"> Reply </button>
</form>
將type="submit"
添加到您的button
,並將您的ReplyAnswer
從button
的onClick
事件移動到form
的onSubmit
事件。 記住也要添加輸入的name
屬性。
然后在您的ReplyAnswer
中,執行以下操作:
function ReplyAnswer(e) {
e.preventDefault();
var data = new FormData(e.target);
var rcontent = data.get("content"); // your input `name` property is `content`
var date = moment().utcOffset('+05:30').format('hh:mm A DD-MM-YYYY');
firebase.firestore().collection('qna2dreply').add({
rcontent,
replyby: userName,
replybyid: userID,
replytoid: props.id,
time: new Date(),
time2: date
}).then(() => {
document.getElementById('xxx').value = '';
swal("Successfully Replied to "+props.title);
});
}
FormData
接口提供了一種方法來輕松構造一組表示表單字段及其值的鍵/值對。 在此處閱讀更多信息: https://developer.mozilla.org/en-US/docs/Web/API/FormData 。
e.target.value 未定義,因為事件 (e) 沒有從事件處理程序 (ReplyAnswer) 傳遞。
只需更改: button onClick={() => { ReplyAnswer() } }
To: button onClick={( e ) => ReplyAnswer( e ) }
這確保事件正確傳遞。 注意花括號被移除以防止從單擊處理程序返回未定義。
干杯,
function ReplyAnswer(value) { var date = moment().utcOffset('+05:30').format('hh:mm A DD-MM-YYYY'); firebase.firestore().collection('qna2dreply').add( { rcontent:value, replyby: userName, replybyid: userID, replytoid: props.id, time: new Date(), time2: date }).then(() => { document.getElementById('xxx').value = ''; swal("Successfully Replied to "+props.title); }); }
1.add an id attribute in your input element eg:<input type="text" id="your_field_id" value="hellow"/> 2. Write your action button like this: <button onClick={(e)=>ReplyAnswer(document.getElementById("your_field_id").value)}>Click</button> 3. You can use REFDOM follow this link https://reactjs.org/docs/refs-and-the-dom.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.