react-hooks

How to use map function for hooks useState properties dynamically

。_饼干妹妹 提交于 2021-02-10 04:58:09
问题 My question is almost the same as this one. In this case, the person has a map of states created in a hard coded way: const App = props => { const [state, changeState] = useState({ name: "", eventTitle: "", details: "", list: "", toggleIndex: "", editName: "", editEventTitle: "", editDetails: "", }); The difference is that I want to create those states dynamically, receiving them from another component. I tried something like this but it obviously did not work: const App = props => { const

Usage of useCallback and setting new object state using previous state as argument

旧街凉风 提交于 2021-02-10 04:56:25
问题 Consider this basic form fields component with a custom form hook to handle input changes: import React, { useState, useCallback } from 'react'; const useFormInputs = (initialState = {})=> { const [values, setValues] = useState(initialState); const handleChange = useCallback(({ target: { name, value } }) => { setValues(prev => ({ ...prev, [name]: value })); }, []); const resetFields = useCallback(() => setValues(initialState), [initialState]); return [values, handleChange, resetFields]; };

Fetch data with a custom React hook

拜拜、爱过 提交于 2021-02-09 07:29:11
问题 I'm newbie in React but I'm developing an app which loads some data from the server when user open the app. App.js render this AllEvents.js component: const AllEvents = function ({ id, go, fetchedUser }) { const [popout, setPopout] = useState(<ScreenSpinner className="preloader" size="large" />) const [events, setEvents] = useState([]) const [searchQuery, setSearchQuery] = useState('') const [pageNumber, setPageNumber] = useState(1) useEvents(setEvents, setPopout) // get events on the main

How to update with React Hooks specific value of an array inside an object?

筅森魡賤 提交于 2021-02-09 00:39:57
问题 I have an object which i want to update it using React Hooks const [rowEdit, setRowEdit] = useState({ rowEdit: { "1x0": [1, 1, 1, 1, 1] } }); I loop through the array with .map, so i have the index for each element. How can i make the second value equal to 0? 回答1: You can return 0 if the map index is equal to 1 , or return the current element otherwise. Example const { useState } = React; function App() { const [rowEdit, setRowEdit] = useState({ rowEdit: { "1x0": [1, 1, 1, 1, 1] } });

How to update with React Hooks specific value of an array inside an object?

给你一囗甜甜゛ 提交于 2021-02-09 00:38:46
问题 I have an object which i want to update it using React Hooks const [rowEdit, setRowEdit] = useState({ rowEdit: { "1x0": [1, 1, 1, 1, 1] } }); I loop through the array with .map, so i have the index for each element. How can i make the second value equal to 0? 回答1: You can return 0 if the map index is equal to 1 , or return the current element otherwise. Example const { useState } = React; function App() { const [rowEdit, setRowEdit] = useState({ rowEdit: { "1x0": [1, 1, 1, 1, 1] } });

How to avoid rerender all child components which in loop when parent component state update

烈酒焚心 提交于 2021-02-08 16:57:40
问题 I m having one child component which is inside a loop of parent component. when one of the child components is updating the state of parent component, it is re-rendering the all children since it is loop. How can i avoid the re-render for each iteration. function Parent() { const [selectedChild, setSelectedChild] = useState([]); const onChangeHandle = (event, id) => { const checked = event.target.checked; let updatedArray = [...selectedChild]; if(checked){ if(!selectedChild.includes(id)){

How to avoid rerender all child components which in loop when parent component state update

岁酱吖の 提交于 2021-02-08 16:57:22
问题 I m having one child component which is inside a loop of parent component. when one of the child components is updating the state of parent component, it is re-rendering the all children since it is loop. How can i avoid the re-render for each iteration. function Parent() { const [selectedChild, setSelectedChild] = useState([]); const onChangeHandle = (event, id) => { const checked = event.target.checked; let updatedArray = [...selectedChild]; if(checked){ if(!selectedChild.includes(id)){

React Hook - Only listen to window *width* size change

懵懂的女人 提交于 2021-02-08 11:43:24
问题 I have a hook that listens to the window.resize event. I wish to only listen to and update when window.innerWidth changes. I wish to ignore changes to window.innerHeight since this gets triggered when opening the soft keyboard. The problem is that the mediaSize is stored in my Redux Store causing the app to re-render when the soft keyboard opens. My code triggers on both window.innerWidth and window.innerHeight changes, how can I change this behaviour? My hook import { useState, useEffect }

Can't fetch firebase collection with React

女生的网名这么多〃 提交于 2021-02-08 10:07:50
问题 I'm using React hooks + firebase. In firebase, I created a collection called "projects" When I try to console log the data with: const Dashboard = () => { const { firebase } = useContext(GlobalContext); return ( <div className="container"> {console.log(firebase.db.collection("projects"))} </div> ); }; I'm getting the following object: Shouldn't be able to see my data? Below are my firebase and context set up firebase.js import app from "firebase/app"; import "firebase/auth"; import "firebase

React Hooks useReduce or useState

纵然是瞬间 提交于 2021-02-08 09:23:20
问题 I have a form with 30 fields and all those fields are a template from a value in a dropdown, if I change the value of the template I will end up creating a custom template. So the action is: When we are changing a value from the form, the template will change in custom template As you can see there are some logic that come up and down and I'm worried about the multiple setState declaration and call. Should I use useReducer or useState ? 回答1: From the useReducer documentation: useReducer is