reactjs - 无法在反应中执行段落的 onLoad 事件?(或:如何在加载 p 元素并将其发送到 Firebase 时获取属性?)
问题描述
我正在设置一个React.js应用程序,其中每个组件都是文章的不同章节。我想收听章节段落元素的加载事件并将其日期提交到Google Firebase 实时数据库,所以我需要获取每个章节的名称,我认为使用属性会更好。我的代码确实适用于onClick
事件,但不适用于onLoad
:
// Epilogue.jsx
import React, { Component } from 'react';
import {getName} from './clicksCounters';
class Epilogue extends Component {
render () {
return <p part='epilogue' onLoad={getName}> // the attribute i am trying to get is "part".
i am the epilogue chapter!
</p>
}
}
export default Epilogue;
// clicksCounters.jsx
import * as firebase from 'firebase';
function getName(e) {
let refTitle = e.currentTarget.part; //the attribute is taken to here.
submitWords();
function submitWords(){
const submittedWords = firebase.database().ref('clicks/chapter/' + refTitle); //this is where i want it, as a subdirectory.
saveWords();
function saveWords() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '/' + mm + '/' + dd;
const newMessageRef = submittedWords.push();
newMessageRef.set({
Date: today + ' ; ' + Date()
});
}
}
}
export {getName};
当我使用()
(例如:)onLoad={getName()}
时,我收到此错误:TypeError: Cannot read property 'currentTarget' of undefined。
有人知道如何解决这个问题吗?
非常感谢!♥</p>
解决方案
推荐阅读
- javascript - 在 UI-Grid cellTemplate 中使用 {{COL_FIELD}}
- vba - 如果文件夹不在数据库中,则从文件夹访问导入数据
- python - 如何检测这个虚线?
- kubernetes - 如何升级 Docker Desktop for MacOS 中嵌入的 Kubernetes 版本?
- java - 如何从 GPSD 检索数据并在 Android 客户端中使用它们
- pycharm - pycharm 中的 Jupyter 笔记本在 Pycharm 中没有本地历史支持?
- java - Nativescript扩展java类生成未知构造函数
- mysql - 添加计算字段的结果
- python - 如何从我美丽的汤结果中删除标签(如:地址 = [a,b,c,d,r......])
- python - 访问字典中的熊猫面具