javascript - 如何在 react.js 的功能组件中获取输入值?(类型错误:无法读取未定义的属性“目标”)
问题描述
我正在尝试将数据添加到 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>
);
}
请帮忙
..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ……
解决方案
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
推荐阅读
- php - 如何在php中以数组形式打印输出?
- php - Laravel/MYSQL created_at 和 updated_at 应用不同的时区
- python - 为什么“找不到帐户”行不起作用?仍然是编码初学者,并尝试过 if 和 else 语句
- node.js - 如何使用 sdk v4 for node js 重新启动与机器人的连接
- javascript - 以编程方式注册要销毁的组件?
- c++ - 如何合并(混合)两个立体声 mp3 音频文件
- python - Pandas date_range 每周从一周中的特定日期开始
- android - 为什么 AndroidX 需要 compileSdkVersion 设置为 28 或更高
- advanced-custom-fields - Advanced Custom Fields Pro - 每个页面都需要唯一的字段组?
- python - Pip 安装日志在哪里?