admin管理员组文章数量:1430083
I'm just starting to use react hooks and I'm having some issues when using custom hooks. It's probably lack of understanding but here's what I'm attempting
My Custom hook:
import React, { useState } from "react"
export const useValidateContent = initState => {
const[valid, setValid] = useState(initState)
const[errorMsg, setErrorMsg] = useState(null)
const validate = () => {
// Update ponent state to test
setValid(false)
setErrorMsg('Some error found')
}
return [valid, validate, errorMsg]
}
My parent container which uses the custom hook:
import React, { useState, useEffect } from 'react'
import { useValidateContent } from './hooks/useValidateContent'
export default function ParentComp () {
const [contentIsValid, validate, contentError] = useValidateContent(true)
const initValidate = () => {
// values before running validate
console.log('valid', contentIsValid)
console.log('error', contentError)
validate()
// values after running validate
console.log('valid', contentIsValid)
console.log('error', contentError)
}
return (
<div>
<button onclick={initValidate} />
</div>
)
}
What I expected to be consoled here was:
valid true
error null
valid false
error Some error found
Instead what I see is:
valid true
error null
valid true
error null
It seems like the hook is not updating the local state. Why is this? Even when I try to console those values inside the hook ponent I get the same thing. I cannot figure out why this is. Am I using custom hooks wrong?
I'm just starting to use react hooks and I'm having some issues when using custom hooks. It's probably lack of understanding but here's what I'm attempting
My Custom hook:
import React, { useState } from "react"
export const useValidateContent = initState => {
const[valid, setValid] = useState(initState)
const[errorMsg, setErrorMsg] = useState(null)
const validate = () => {
// Update ponent state to test
setValid(false)
setErrorMsg('Some error found')
}
return [valid, validate, errorMsg]
}
My parent container which uses the custom hook:
import React, { useState, useEffect } from 'react'
import { useValidateContent } from './hooks/useValidateContent'
export default function ParentComp () {
const [contentIsValid, validate, contentError] = useValidateContent(true)
const initValidate = () => {
// values before running validate
console.log('valid', contentIsValid)
console.log('error', contentError)
validate()
// values after running validate
console.log('valid', contentIsValid)
console.log('error', contentError)
}
return (
<div>
<button onclick={initValidate} />
</div>
)
}
What I expected to be consoled here was:
valid true
error null
valid false
error Some error found
Instead what I see is:
valid true
error null
valid true
error null
It seems like the hook is not updating the local state. Why is this? Even when I try to console those values inside the hook ponent I get the same thing. I cannot figure out why this is. Am I using custom hooks wrong?
Share Improve this question asked Mar 28, 2019 at 19:17 bos570bos570 1,5234 gold badges25 silver badges49 bronze badges 5-
1
Updating state with hooks is asynchronous just like
setState
in a class ponent is, and since the state is not mutatedcontentIsValid
andcontentError
will still refer to the stale old state and not the new state. – Tholle Commented Mar 28, 2019 at 19:27 -
@Tholle I don't quite follow. I though the
setValid
andsetErrorMsg
were supposed to mutate the state. – bos570 Commented Mar 28, 2019 at 19:31 -
It updates the state, but it doesn't update it in place like e.g. using the
push
method on an array does. The values will be updated in the next render. – Tholle Commented Mar 28, 2019 at 19:32 - 1 @Tholle ahh, that makes so much more sense now. Thanks! – bos570 Commented Mar 28, 2019 at 19:35
- Great! You're wele. – Tholle Commented Mar 28, 2019 at 19:36
2 Answers
Reset to default 2Updating state with hooks is asynchronous just like setState
in a class ponent is, and since the state is not mutated contentIsValid
and contentError
will still refer to the stale old state and not the new state.
If you render your state variables you will see that your code works as expected.
const { useState } = React;
const useValidateContent = initState => {
const [valid, setValid] = useState(initState);
const [errorMsg, setErrorMsg] = useState("");
const validate = () => {
setValid(false);
setErrorMsg("Some error found");
};
return [valid, validate, errorMsg];
};
function ParentComp() {
const [contentIsValid, validate, contentError] = useValidateContent(true);
const initValidate = () => {
// values before running validate
console.log("valid", contentIsValid);
console.log("error", contentError);
validate();
// values after running validate
console.log("valid", contentIsValid);
console.log("error", contentError);
};
return (
<div>
<button onClick={initValidate}>initValidate</button>
contentIsValid: {contentIsValid.toString()}, contentError: {contentError}
</div>
);
}
ReactDOM.render(<ParentComp />, document.getElementById("root"));
<script src="https://unpkg./react@16/umd/react.development.js"></script>
<script src="https://unpkg./react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
valid state is set when you called validate() function and since the custom hook return valid state value to the ponent you use it at, you can directly use valid state.
The problem is, when you called validate() and "valid" got its state changed, but our ponent needs to tell when valid gets a value assign render our ponent. So in react functional poennts we can simply put "valid" as a dependency for useEffect. then whenever valid gets state it will call a re render for our ponent.
本文标签: javascriptComponent local state not updating with react custom hooksStack Overflow
版权声明:本文标题:javascript - Component local state not updating with react custom hooks - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745552164a2662991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论