首页 > 解决方案 > 无法在反应中执行段落的 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>

标签: reactjsfirebaseattributesonloadparagraph

解决方案


推荐阅读