reactjs

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

时光毁灭记忆、已成空白 提交于 2021-02-10 04:58:49
问题 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]; };

Can't setState Firestore data

放肆的年华 提交于 2021-02-10 04:58:33
问题 I'm working on a React project with Cloud Firestore. I have successfully fetched data from Firestore. But I could not set state these data to state. How can I set state these data. class App extends Component { constructor(props) { super(props); this.state = { items: [] }; } async componentDidMount() { const items = []; firebase .firestore() .collection("items") .get() .then(function(querySnapshot) { querySnapshot.forEach(function(doc) { items.push(doc.data()); }); }); this.setState({ items:

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

我的梦境 提交于 2021-02-10 04:58:10
问题 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]; };

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]; };

onMouseEnter and onMouseLeave not functioning as expected

余生颓废 提交于 2021-02-10 04:55:11
问题 I'm trying to simulate a hover effect for my component. However, the onMouseEnter / Leave events are not working as expected, right now I'm trying to get it to simply console.log a string to check that its working but nothing is happening. The aim is that I can change its background color on hover; I tried setting this through a CSS class:hover but to no avail. How do I get the OnMouseEnter / onMouseLeave to function properly? here's the code: import React from "react" import ReactDOM from

Images in React: is it possible to use a string from an imported object as the source path for an image?

﹥>﹥吖頭↗ 提交于 2021-02-10 04:15:52
问题 Question about using images in React! Importing a pic and then using it works: import photo from "../img/profilephoto.jpg" <img src={photo} /> Using props as the source inside a require() works: <img src={require(`../img/${this.props.img}`)} /> But importing an object (with more objects), and then using navigation to get a string, does not work! import text from "../text" // This is the object wich contains more objects <img src={require(`../img/${text.contact.photo}`)} /> Error because "text

Images in React: is it possible to use a string from an imported object as the source path for an image?

扶醉桌前 提交于 2021-02-10 04:15:08
问题 Question about using images in React! Importing a pic and then using it works: import photo from "../img/profilephoto.jpg" <img src={photo} /> Using props as the source inside a require() works: <img src={require(`../img/${this.props.img}`)} /> But importing an object (with more objects), and then using navigation to get a string, does not work! import text from "../text" // This is the object wich contains more objects <img src={require(`../img/${text.contact.photo}`)} /> Error because "text

Images in React: is it possible to use a string from an imported object as the source path for an image?

邮差的信 提交于 2021-02-10 04:14:38
问题 Question about using images in React! Importing a pic and then using it works: import photo from "../img/profilephoto.jpg" <img src={photo} /> Using props as the source inside a require() works: <img src={require(`../img/${this.props.img}`)} /> But importing an object (with more objects), and then using navigation to get a string, does not work! import text from "../text" // This is the object wich contains more objects <img src={require(`../img/${text.contact.photo}`)} /> Error because "text

How to handle React Router with Node Express routing

夙愿已清 提交于 2021-02-10 04:14:02
问题 I am trying to manage a react app with react router and node js server my router in react : <BrowserRouter> <Switch> <PrivateRoute token={token} Component={Payments} exact path="/payments"/> <PrivateRoute token={token} Component={User} exact path="/user"/> <PrivateRoute token={token} Component={User} exact path=""/> <PrivateRoute token={token} Component={User} exact path="/"/> </Switch> <BrowserRouter/> export const PrivateRoute = ({Component, ...rest,token}) => { return ( <Route {...rest}