-1

I am new to React js and I am implementing the useEffect for the first time in my code. I followed few tutorials and they have done things differently as follows

Case l:

function App() {

  const[counter,setCounter] = useState(0);
  const[toggle,setToggle] = useState(false); 

  const incrementer = () =>{
    // counter+=1
    setCounter(counter + 1);    
    console.log(counter)
  };

  const toggler = () =>{
    setToggle((toggle) => !toggle);
  };
  
  return (
    <div className="App">
      <h1 className = {toggle ? 'active' : ''} >Hello world!!</h1>
      <h1>{counter}</h1>
      <h1>{abc}</h1>
      <button onClick={incrementer} >Click</button>
      <button onClick={toggler}>Toggle Here</button>     
      
      
      <div className="home">
        <h1>{Date.time}</h1>
        <Nav/> 
        <Tweets/>
      </div>    
    </div>
  );
}

Here inside the incrementer function, I am just using counter+1 and it gives the required result and makes sense. It increases the count by 1 on every click.

Now case 2:

function App() {  

  const[counter,setCounter] = useState(0);
  const[toggle,setToggle] = useState(false);
  

  const incrementer = () =>{
    
    setCounter(() => counter+1);
    console.log(counter)
  };

  const toggler = () =>{
    setToggle((toggle) => !toggle);
  };
  
  return (
    <div className="App">
      <h1 className = {toggle ? 'active' : ''} >Hello world!!</h1>
      <h1>{counter}</h1>
      <h1>{abc}</h1>
      <button onClick={incrementer} >Click</button>
      <button onClick={toggler}>Toggle Here</button>    
          
      <div className="home">
        <h1>{Date.time}</h1>
        <Nav/> 
        <Tweets/>
      </div>    
    </div>
  );
}

In this case, inside the incrementer function, it is using what seems like an arrow function. I assume it is using function inside the function. Also, it is not passing anything as () is empty. How the count is increasing here. I am confused.

Also inside the toggler function, I can write abc or any variable instead of toggle and it works. How??

Update

Ok my main confusion is in this code.

const incrementer = () =>{
    // counter+=1
    setCounter((kkk) => kkk + 1);
    // setCounter(() => counter+1);
    console.log(counter)
  };

This function works as well. How is this kkk value is getting the value of counter=0. Shouldnt we be writing counter instead of kkk??

Reactoo
  • 916
  • 2
  • 12
  • 40
  • 1
    "*it is using what seems like an arrow function.*" - yes. But an unnecessary one - it always returns a constant value. "*I assume it is using function inside the function*" - not sure what you mean? – Bergi Jun 12 '21 at 14:33
  • 1
    "*I can write abc or any variable instead of toggle and it works. How?*" - it's a function parameter. You get to name it, the caller of the function does not care. – Bergi Jun 12 '21 at 14:34
  • For you first reply, isnt setCounter one function and () is another function without name?? – Reactoo Jun 12 '21 at 14:37
  • Yes, `setCounter` is a function, `() => counter+1` is a function, and so are `incrementer` or `App`. – Bergi Jun 12 '21 at 14:39
  • that means () => counter+1 is a function inside setCounter function . A function inside another function. is that true?? – Reactoo Jun 12 '21 at 14:40
  • 1
    A function that is passed as an argument to a call of a function. I wouldn't call that "*a function inside a function*" - I would use that phrase to say "`incrementer` is defined inside `App`". – Bergi Jun 12 '21 at 14:44
  • You'll want to take a look at https://stackoverflow.com/questions/57828368/why-react-usestate-with-functional-update-form-is-needed btw for the difference between the two `setState` calls. Notice this has *nothing* to do with arrow functions vs normal functions. – Bergi Jun 12 '21 at 14:49

1 Answers1

1

Not only react hooks could use arrow functions in this way, but almost all cases in Javascript. Maybe we could have explanation in Comparing traditional functions to arrow functions

Chris Wong
  • 564
  • 4
  • 4
  • I have added the update @Chris. Can you have a look at it?? – Reactoo Jun 12 '21 at 15:13
  • It is about [Functional updates](https://reactjs.org/docs/hooks-reference.html#functional-updates). If the new state is computed using the previous state, you can pass a function to setState. The function will receive the previous value, and return an updated value. – Chris Wong Jun 14 '21 at 05:52