reactjs

How to create dynamic refs in functional component- Using useRef Hook

蹲街弑〆低调 提交于 2021-02-18 13:52:55
问题 I want to have refs in functional component which have elements dynamically rendered. please help me in creating Dynamic Refs using useRef Hook and accessing in the handler. 1) Need to create 3 useRefs to point 3 buttons. 2) Access them in button handler using "ref1.value" Or "ref2.value" etc let abc=[1,2,3]; function submitclick(){ alert(123); // Here i want to access the buttons with using the refs } return ( <div> { abc.map(function(index){ return (<button ref='123' onClick={submitclick}>{

How to create dynamic refs in functional component- Using useRef Hook

我是研究僧i 提交于 2021-02-18 13:51:31
问题 I want to have refs in functional component which have elements dynamically rendered. please help me in creating Dynamic Refs using useRef Hook and accessing in the handler. 1) Need to create 3 useRefs to point 3 buttons. 2) Access them in button handler using "ref1.value" Or "ref2.value" etc let abc=[1,2,3]; function submitclick(){ alert(123); // Here i want to access the buttons with using the refs } return ( <div> { abc.map(function(index){ return (<button ref='123' onClick={submitclick}>{

Adding dynamic states in reactJs

痴心易碎 提交于 2021-02-18 12:56:06
问题 I created a reactJs page that allows an admin add a user to a platform. Now, instead of submitting the form for each new user, the admin can add as many users as possible before submitting the form. By default, one table row containing input fields is displayed and then on click of the add button, a new row is added and the admin can fill the necessary details. I want to add an onChange event to the input fields for each new row. I don't know how to do as this is quite dynamic. The state will

Delay React onMouseOver event

社会主义新天地 提交于 2021-02-18 12:41:14
问题 I have a list of elements, when hovering one of these, I'd like to change my state. <ListElement onMouseOver={() => this.setState({data})}>Data</ListElement> Unfortunately, if I move my mouse over the list, my state changes several times in a quick succession. I'd like to delay the change on state, so that it waits like half a second before being fired. Is there a way to do so? 回答1: Here's a way you can delay your event by 500ms using a combination of onMouseEnter , onMouseLeave , and

material-ui makeStyles: how to style by tag name?

梦想的初衷 提交于 2021-02-18 11:47:08
问题 I want to add a rule for all <p> in the current component. I couldn't find information anywhere (material-ui documentation, Stack Overflow, etc.) on how to add CSS rules by tag name. Is it not supported? I'm trying to do something like this: const useStyles = makeStyles((theme: Theme) => createStyles({ 'p': { margin: 0, }, someClass: { fontSize: 14, }, }) ); EDIT: Using Ryan's solution works, but creates a new problem: import React from 'react'; import { makeStyles, Theme, createStyles } from

material-ui makeStyles: how to style by tag name?

ぐ巨炮叔叔 提交于 2021-02-18 11:47:07
问题 I want to add a rule for all <p> in the current component. I couldn't find information anywhere (material-ui documentation, Stack Overflow, etc.) on how to add CSS rules by tag name. Is it not supported? I'm trying to do something like this: const useStyles = makeStyles((theme: Theme) => createStyles({ 'p': { margin: 0, }, someClass: { fontSize: 14, }, }) ); EDIT: Using Ryan's solution works, but creates a new problem: import React from 'react'; import { makeStyles, Theme, createStyles } from

Disable ContextMenu in ReactJS

混江龙づ霸主 提交于 2021-02-18 10:34:20
问题 first post here so hopefully I can ask this question the most helpful manner possible. I'm pretty new to coding and in trying to push myself decided to attempt to recreate Minesweeper using React and not using any tutorial. I've gotten a lot of the functionality but am really stuck on this part. I am using the event listener 'onContextMenu' to register a right click to "flag" a mine in the program. But I can't figure the right way to isolate it or maybe it's a syntax issue preventing the menu

React Hooks: What is the difference between 'useMutationEffect' and 'useLayoutEffect'?

一曲冷凌霜 提交于 2021-02-18 10:30:27
问题 What is the difference between useMutationEffect and useLayoutEffect in term of usage? I have read all available material online including (but not limited to) Hooks Reference Blog post by Kent Difference between useEffect and other two hooks is clear. but difference between useMutationEffect and useLayoutEffect is still not clear. I know the order of execution is: useMutationEffect useLayoutEffect useEffect 回答1: First, you have to understand the different phases of Rendering. A DOM mutation

Re-render component when navigating the stack with React Navigation

落花浮王杯 提交于 2021-02-18 10:19:14
问题 I am currently using react-navigation to do stack- and tab- navigation. Is it possible to re-render a component every time the user navigates to specific screens? I want to make sure to rerun the componentDidMount() every time a specific screen is reached, so I get the latest data from the server by calling the appropriate action creator. What strategies should I be looking at? I am pretty sure this is a common design pattern but I failed to see documented examples. 回答1: React Navigation

What is the purpose of `beforeEach` global in Jest?

狂风中的少年 提交于 2021-02-18 10:16:06
问题 I always find Jest-Enzyme test cases starting with a beforeEach global that resembles like the following: describe('TEST BLOCK' () => { let wrapper; beforeEach(() => { wrapper = shallow(<Component />); )); )); The function inside the beforeEach global runs before each test inside the TEST BLOCK describe block. In this case, it shallow renders the Component and assigns to wrapper before running each test. I am not quite sure why do we do this in the first place. Are we not deliberatly slowing