简体   繁体   English

如何将参数从 React 组件传递给事件

[英]How to pass parameter to event from React Component

I have this two files, WordList.js:我有这两个文件,WordList.js:

import React, { useEffect, useState } from 'react';
import WordListItem from "./WordListItem";

function WordList(props) {
    var cards = props.cards;
    var cardsElements = null;

    var [ id, setId ] = useState('');

    function contextMenuClickHandler(id) {
        setId(id);
        console.log(id);
    }

    useEffect(() => {
        document.addEventListener('click', function() {console.log(this)}.bind(id));
    }, []);

    if (cards != null) {
        cardsElements = cards.map( card =>
            <WordListItem
                value={card.original_word}
                key={ card.id }
                id={ card.id }
                visible={ card.id === id }
                contextMenuClickHandler={ contextMenuClickHandler }
            />
        );
    }

    return (
        <React.Fragment>
        <button onClick={ contextMenuClickHandler }>kek</button>
            { cardsElements }
        </React.Fragment>
    )
}

export default WordList

And WordListItem.js:和 WordListItem.js:

import React, { useState, useEffect } from 'react';
import './WordListItem.css';
import more from './more.svg';
import ContextMenu from './ContextMenu'

function WordListItem(props) {
    function contextMenuClickHandler(event) {
        props.contextMenuClickHandler(props.id);
    }

    return (
        <p className="word-list-item">
            { props.value }
            { props.visible ? <ContextMenu/> : null }
            <img src={ more } className="more-button" onClick={ contextMenuClickHandler } />
        </p>
    );
}

export default WordListItem;

First component - WordList is a list of WordListItem, and every WordListItem have ContextMenu component that displays when user clicks on img element.第一个组件 - WordList 是一个 WordListItem 列表,每个 WordListItem 都有一个 ContextMenu 组件,当用户单击 img 元素时会显示该组件。 WordList stores id of WordListItem that must displays ContextMenu in concrete moment to have displayed only one ContextMenu in any time. WordList 存储必须在具体时刻显示 ContextMenu 的 WordListItem 的 id,以便在任何时间只显示一个 ContextMenu。 To close this ContextMenu user must click to one of ContextMenu buttons.要关闭此 ContextMenu 用户必须单击 ContextMenu 按钮之一。 But i want that if user click in another space of document (any place) close ContextMenu too.但是我希望如果用户单击文档的另一个空间(任何地方)也关闭 ContextMenu。 In useEffect on WordList i add listener (with [] because document must have only one handler).在 WordList 上的 useEffect 中,我添加了侦听器(使用 [],因为文档必须只有一个处理程序)。 I can pass parameter with id to documentClickHandler but parameter remains the same value after i update id.我可以将带有 id 的参数传递给 documentClickHandler 但参数在我更新 id 后保持相同的值。 In answers people say me to write useEffect to update id.在回答中人们说我写 useEffect 来更新 id。 But how can i call useEffect?但是我怎么能调用useEffect呢?

Added.添加。

i made this:我做的:

    useEffect(() => {
        document.addEventListener('click', function() {console.log(this())}.bind(test));
    }, []);

    function test() {
        return Math.random();
    }

it works, handler have new value ever click, but if i make this:它有效,处理程序有新的价值,但如果我这样做:

    function test() {
        return id;
    }

where id i get from his:我从他那里得到的 id:

    var [ id, setId ] = useState('');

And with state id, it doesn't work.使用状态 ID,它不起作用。 I must use useEffect but how must i do this?我必须使用 useEffect 但我必须如何做到这一点?

When you set the dependency list of the useEffect hook as empty array, then the function someFunction will be called only on first render.当您将 useEffect 钩子的依赖项列表设置为空数组时,则函数someFunction将仅在第一次渲染时调用。

let the useEffect watch id and call the function whenever the id changes as following让 useEffect 监视id并在id更改时调用该函数,如下所示

useEffect(someFunction, [id])

And also call setId function whenever you want to change id并且在您想更改id时也调用setId函数

将 id 添加为 useEffect 的依赖项,这样单击事件侦听器将在 id 更改时更新:

useEffect(someFunction, [id]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何传递“参数”来反应组件 - How to pass "parameter" to react component 当自定义参数从子组件获取数据时,如何在 React 中除了您自己的自定义参数之外传递事件参数 - How to pass an event parameter in addition to your own custom parameters in React when the custom parameter is getting data from child component 如何将参数从 useEffect 传递给 React 中的子组件? - How to pass a parameter from useEffect to child component in React? 如何在 React 功能组件中的 onClick 上传递参数 - How to pass parameter on onClick in React Functional Component 如何在反应中将获取参数数据传递给组件? - How to pass get parameter data to a component in react? “React”如何将事件传递给相邻组件 - “React” how to pass event to adjacent component 如何在React中将事件处理程序传递给子组件 - How to pass an event handler to a child component in React 在 React 中,如何将参数从子组件传递到父组件? - In React, how do a pass a parameter from a child component up to a parent component? 如何在 React 中将事件从子组件传递到父组件返回到另一个子组件 - how to pass an event from a child component to parent component back down to another child in React 如何使用react将event.target.value从一个组件传递到另一个组件 - How to pass the event.target.value from one component to another component using react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM