[英]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.