score:2
To answer your question:
how to calculate default value for react js state hook with a function?
useState allows a function as a 'initial state factory', like e.g.:
const [ state, setState ] = useState( function(){ return Math.random(); } );
So if you want to use your MyFunction
as a factory, just use it this way:
const [ state2, setState2 ] = useState( MyFunction );
why is MyFunction called on every click ?
A React functional component is just a javascript function.
React decides when to call this function, which is basically whenever something changes. A call of some setState()
is one reason why
React will call the function of the functional component again (your App()
function in this case).
But I suggest you consider the App()
function to be called "whenever React wants to call it", or "all the time, again and again". Meaning you should not
rely on when the function of the functional component is called, you should instead rely on the guarantees which React makes
regarding when the state is up-to-date, specifically useEffect
, useState
, ...
MyFunction()
is just a function call, which is inside the App()
function call, so - of course - MyFunction()
is called
whenever App()
is called (which is "again and again").
Why is the alert()
called twice ?
The functional component is called 2 times in Strict Mode. This causes unexpected behavior only if you aren't using React as it is supposed to be used (which is something that just happens for React-beginners).
If you are using React in the intended way, you should not have to care about if the function is called once, twice or multiple times. The only thing that counts is what the state is.
See also e.g. React.Component class constructor runs once without console.log?
Source: stackoverflow.com
Related Query
- how to calculate default value for react js state hook with a function?
- How can ı pass function with state for react useContext
- How to check if state default boolean false is changing to true with React Hook useEffect
- how do i update state in react for updating the value of an object inside an array using array.map function
- How to specify a function as an optional parameter to a hook with a default value
- How to use a state value with map function to display repeat ui in react component
- How to use useState hook with array state for children that call the setter function without infinite rendering loop?
- How to get the changed (new) state value in React JS function with / without useEffect hook?
- How to specify (optional) default props with TypeScript for stateless, functional React components?
- How to specify function parameters for React component callback with TypeScript?
- Providing default value for function prop in React
- How to use React Context with useState hook to share state from different components?
- How to receive select value in handlesubmit function with formik react js?
- ReactJS - How can I implement pagination for react with keep state when route change?
- Sync queryParameters with Redux state and react router for function components
- How to set initial state value for useState Hook in jest and enzyme?
- How to reset React hook state with setTimeout in useEffect
- How do I set a default value for a select menu in my React form component?
- Default function parameter value from React component state
- How to set key/value default state for TypeScript React app
- How to update State Hook with onClick listener on React Component Instance
- How to use router for switching between components with a key value pair in react
- How do I execute function after loading all external library with React Hook useEffect?
- show default value in select input with backgroundcoulr different for each : react js
- How to delete objects from react state hook array with a button click
- How to use map function on react state hook
- How to use an enum in typescript as a default prop value for a react component
- How to pass react hook state to another component for each click
- React warning: contains an input of type hidden with value and default value - how to fix?
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
More Query from same tag
- Why this.props returns old data?
- how to call a function from child components to parent component(function is in parent) without including the full component
- New to react...state not updating on login
- react-router-transition - how to get custom transition?
- Generating Boostrap 4 nav from json data in react
- Styling components and react-bootstrap
- Axios promise will never resolve
- How to read an image from the localhost url and get its width and height in javascript
- Send locale.storage email and new email axios post request
- How to map a nested data object in React?
- react-slideshow-image package not loading images from google drive
- How to get submit form button to be on the next line css?
- Understanding documentation of React with redux
- TypeScript error inside JavaScript file in VSCode
- why req is undefined after Router.push('url') call . (Unhandled Rejection (TypeError): Cannot read property 'headers' of undefined.)
- Reduce an array based on more than one value
- Pass props between components using state hook
- Add up total of array values multiplied by input value in react component
- React Redux - Mapping over array while rerendering only components holding changed object (in array)
- (node:15976) UnhandledPromiseRejectionWarning when trying to connect to mongodb
- NPM manual review not working after updating packages
- What could be causing redux state load delay?
- Editable combo box in Redux-Form
- Is there any way to get a dynamic icon with Semantic-UI?
- after validation call save method
- Drop shadow for a line in react native
- Can I create a file with functions only and import it in an App when using React?
- Is there any difference between rendering a functional component with and without hooks?
- How to impact different class or HTML elements when hovering over a button?
- Google Analytics on React app doesn't work