admin管理员组文章数量:1403461
function useState(initVal){
const state = initVal
const setState = (newVal) => {
state = newVal
}
}
const [state,setState] = useState(0)
console.log(state)
TypeError: useState is not a function or its return value is not iterable
at /home/runner/ViciousSqueakyProfessionals/index.js:8:26
at Script.runInContext (vm.js:130:18)
at Object.<anonymous> (/run_dir/interp.js:209:20)
at Module._pile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
I can't seem to figure out why i need the return value to be iterable can someone throw some light over return method in all ;-;
function useState(initVal){
const state = initVal
const setState = (newVal) => {
state = newVal
}
}
const [state,setState] = useState(0)
console.log(state)
TypeError: useState is not a function or its return value is not iterable
at /home/runner/ViciousSqueakyProfessionals/index.js:8:26
at Script.runInContext (vm.js:130:18)
at Object.<anonymous> (/run_dir/interp.js:209:20)
at Module._pile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
I can't seem to figure out why i need the return value to be iterable can someone throw some light over return method in all ;-;
Share Improve this question edited Nov 4, 2020 at 4:13 kmoser 9,3183 gold badges26 silver badges44 bronze badges asked Nov 4, 2020 at 4:07 mooy botmooy bot 861 silver badge8 bronze badges2 Answers
Reset to default 4As you are calling the useState
function and using array destructuring on the return value of the function that is why you need you return the array first in the method itself.
You need to return values as array, Also change state to let for reassigning:
function useState(initVal){
let state = initVal
const setState = (newVal) => {
state = newVal
}
return [state, setState];
}
You can use these value in either ways:
const [state, setState] = useState(0);
// or
const value = useState(0);
value[0] // = state
value[1]() // = setState()
The main reason between this and the actual useState
hook is that change the state
value won't take ponent to rerender.
Maybe, it looks like react "useState" hook.
"useState" hook has a one arguments and return array(first one is value, second one is function - change value).
[Yours]
function useState(initVal){
const state = initVal
const setState = (newVal) => {
state = newVal
}
}
const [state,setState] = useState(0) // error - because you are not return any.
console.log(state)
[Correct]
function useState(initVal){
const state = initVal
const setState = (newVal) => {
state = newVal
}
return [state, setState]
}
const resultOfUseState = useState(0);
console.log(resultOfUseState);
console.log(resultOfUseState[0]);
console.log(resultOfUseState[1]);
Maybe, you can understand!
Also, It is possible "resultOfUseState" to const [state, setState] = useState. ( resultOfUseState[0] = state, resultOfUseState[1] = setState )
It calls destructuring.
本文标签: javascriptReturn value is not iterableStack Overflow
版权声明:本文标题:javascript - Return value is not iterable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744364159a2602689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论