score:1
The issue lies in the fact that setting state is an asynchronous process.
In your updateScore
method, you are setting the newly computed strength to the state. But remember that setting a state is asynchronous activity. So you would not get the updated state to compute your strength message, since it hasn't been updated yet.
For this to work properly, instead of using the strength
from the state to compute your message, you can use the return value of passwordStrength
method.
const updateScore = (props) => {
const newStrength = passwordStrength(props);
setStrength(newStrength);
setpwInput(props);
if(newStrength ===0){
setHiddenStatus(true);
setStrengthMessage("We need a strong password");
}
if(newStrength > 0 && newStrength <= 2){
//weak sauce
setStrengthMessage("Weak sauce :(");
}
if(newStrength ===3){
//better
setStrengthMessage("Better...");
}
if(newStrength ===4){
//great
setStrengthMessage("Great");
}
if(newStrength >=5){
//outstanding
setStrengthMessage("Outstanding!");
}
}
score:2
before setting strength try storing the password strength value in a variable and then use it instead of the hook inside function for setting strength message
const updateScore = (props) => {
let temp = passwordStrength(props)
setStrength(temp)
setpwInput(props)
if(temp ===0){
setHiddenStatus(true)
setStrengthMessage("We need a strong password")
}
if(temp > 0 && temp <= 2){
//weak sauce
setStrengthMessage("Weak sauce :(")
}
if(temp ===3){
//better
setStrengthMessage("Better...")
}
if(temp ===4){
//great
setStrengthMessage("Great")
}
if(temp >=5){
//outstanding
setStrengthMessage("Outstanding!")
}
}
Source: stackoverflow.com
Related Query
- Having trouble with React Hooks and States. Trying to make a password strength meter
- React Hooks - having trouble with splice updating state during onClick event
- trying to make a React dropdown with checkbox options in it and its not working properly
- Make focus and blur input with react hooks
- How can I make react render the new state modifications after I have updated the state with hooks and context
- Trying to reset a list before filtering, but having trouble with the asynchronous states
- I am having Trouble Mapping a created array in React and am trying to spread it (but to no avail)
- I'm having some trouble trying to change specific button's value when clicked and get back to the previous value with setTimeout()
- React Hooks useEffect saves the last state and won't update it with the new one. It runs twice with two different states
- Having lots of trouble with React and Redux setup and props
- How to Make A second API call based on the value gotten from the first. with React and useEffect hooks
- I'm having an infinite loop with React hooks and I'm not sure why
- I am building react app with ag-grid and I am having an issue with trying how to put a link in the cell
- How to make react router work with static assets, html5 mode, history API and nested routes?
- Formik, Yup Password Strength Validation with React
- Trouble with simple example of React Hooks useCallback
- In Visual Studio 2012, I get warnings and squiggly lines when trying to write JSX with React JS
- Persistence with localStorage with useState and useContext React Hooks
- How to perform authentication with React hooks and react-router
- Save and retrieve states with redux-persist - React Native
- how to make google bots crawl my react website with dynamic urls and display them in google
- Enzyme errors with React Context and Hooks API
- `Invalid mapping` error trying to use babel require hook and polyfill with react jsx transpile
- FlatList not updating with React Hooks and Realm
- How to handle React Svg Drag and Drop with React Hooks
- Is there such a thing as a "correct" way of defining state with React hooks and Typescript?
- Changing language with React Hooks and Contex API
- Having Issues with Writing Backend Code in Express and Connecting To React
- Having an issue while up and running unit test with react redux testing library
- How to use setTimeout() along with React Hooks useEffect and setState?
More Query from same tag
- If/Else in Payload JSX
- ReactJS : update state regularly in a specific time interval
- Unable to send formData to nodejs server
- componentDidUpdate vs componentWillReceiveProps use case in react
- How to dispatch more than one action in mapDispatchToProps in react-redux
- React resizable only resizing the width of the box, not the height
- react-widget component not being properly rendered
- Refetching a query using Apollo-Client throws an error
- How to set the style a react.js component when creating it?
- Can return directly a component from a handling event function in Reactjs?
- Content disappears when using the <Route> tag
- CSS - How do I create a grid that takes up the remaining width of screen (without counting sidebar)
- 'if () {} else {}' statement in React JSX?
- How to avoid re-render infinite loop from event listener (React)
- Walkaround "Cannot update during existing state transaction" in React
- Primary and secondary font with Chakra UI
- Is there a way I can use button in react to delete a item in an array that is stored in the state
- Socket IO with React and Express Error: "WebSocket connection to '.......' failed: WebSocket is closed before the connection is established."
- How can I use setInterval to change state with or without useEffect?
- Integrating Google Maps api with React js (draggable maker not returning coordinates)
- How to share state between React function and re-render on changes
- Problems installing react bacause of dependencies with babel
- next js react app undefined compilation error
- React router - code splitting lazy imports. Switch with Suspense outside
- How to change spacing system based on screen width Material UI?
- Clicking the button does not work - React
- Next.js with next-redux-wrapper state is being reset to initial value when navigating using Link
- "Invalid hook call." when usin react useState Hook
- Wrapping a legacy widget for ReactJS
- How to get the current theme in a spfx webpart using the new ts based styling approach