How to use `setState` callback on react hooks

You need to use useEffect hook to achieve this. const [counter, setCounter] = useState(0); const doSomething = () => { setCounter(123); } useEffect(() => { console.log(‘Do something after counter has changed’, counter); }, [counter]); If you want the useEffect callback to be ignored during the first initial render, then modify the code accordingly: import React, … Read more

How do I reference a local image in React?

First of all wrap the src in {} Then if using Webpack; Instead of: <img src={“./logo.jpeg”} /> You may need to use require: <img src={require(‘./logo.jpeg’)} /> Another option would be to first import the image as such: import logo from ‘./logo.jpeg’; // with import or … const logo = require(‘./logo.jpeg); // with require then plug … Read more

Infinite loop in useEffect

Passing an empty array as the second argument to useEffect makes it only run on mount and unmount, thus stopping any infinite loops. useEffect(() => { setIngredients({}); }, []); This was clarified to me in the blog post on React hooks at https://www.robinwieruch.de/react-hooks/

How to create a protected route?

Issue <BrowserRouter> <Switch> {authLogin ? ( <> <Route path=”/dashboard” component={Dashboard} exact /> <Route exact path=”/About” component={About} /> </> ) : ( <Route path=”/” component={Login} exact /> )} <Route component={PageNotFound} /> </Switch> </BrowserRouter> The Switch doesn’t handle rendering anything other than Route and Render components. If you want to “nest” like this then you need to … Read more

React setState not Updating Immediately

You should invoke your second function as a callback to setState, as setState happens asynchronously. Something like: this.setState({pencil:!this.state.pencil}, myFunction) However in your case since you want that function called with a parameter you’re going to have to get a bit more creative, and perhaps create your own function that calls the function in the props: … Read more

Updating an object with setState in React

There are multiple ways of doing this, since state update is a async operation, so to update the state object, we need to use updater function with setState. 1- Simplest one: First create a copy of jasper then do the changes in that: this.setState(prevState => { let jasper = Object.assign({}, prevState.jasper); // creating copy of … Read more

What’s the difference between “super()” and “super(props)” in React when using es6 classes?

There is only one reason when one needs to pass props to super(): When you want to access this.props in constructor. Passing: class MyComponent extends React.Component { constructor(props) { super(props) console.log(this.props) // -> { icon: ‘home’, … } } } Not passing: class MyComponent extends React.Component { constructor(props) { super() console.log(this.props) // -> undefined // … Read more

How to get parameter value from query string?

React Router v6, using hooks In react-router-dom v6 there’s a new hook named useSearchParams. So with const [searchParams, setSearchParams] = useSearchParams(); searchParams.get(“__firebase_request_key”) you will get “blablabla”. Note, that searchParams is an instance of URLSearchParams, which also implements an iterator, e.g. for using Object.fromEntries etc. React Router v4/v5, without hooks, generic React Router v4 does not … Read more