【问题标题】:I gets error after implementing useStack hook. error: (stack is undefined)实现 useStack 挂钩后出现错误。错误:(堆栈未定义)
【发布时间】:2022-01-02 11:57:44
【问题描述】:

我有一个数组,它的值转换为按钮。单击每个按钮时,其值将写入 ul 列表中。当点击“Pop”时,最后一个值被删除。

预期输出:

我使用 useStack 钩子来实现它。但是运行后,输出是一个空白页,我在控制台收到这个错误:

Uncaught TypeError: stack is undefined

components.js:

import React from 'react';
import {useStack} from './hooks'

export function Demo() {
    return (
        <div className="demo">
        <StackDemo/>
        </div>
    );
}

const words = ['Apple', 'Banana', 'Cherry', 'Grape'];
function StackDemo() {
    const {stack, push, pop} = useStack();
    return (<div>
        {words.map((word, index) => <button key={index} onClick={() => push(word)}>{word}</button>)}
        <button onClick={pop}>» Pop</button>
        <ul>
            {stack.map((item, index) => <li key={index}>{item}</li>)}
        </ul>
    </div>);
}

hooks.js:

import {useState} from 'react';

export function useStack() {
    const [array,setArray] = useState([]);

    const add = (value) => {
        return setArray(array.push(value));
    }
    const del = () => {
        return setArray(array.pop());
    }

    return {array, add, del};
}

当然,如果我编辑 components.js:

// const {stack, push, pop} = useStack(); to // 常量 [stack, push, pop] = useStack();

并编辑 hooks.js:

// return {array, add, del} to // return [array, add, del]

然后输出显示:

但是当我点击其中一个时,输出将变成空白页,并且我在控制台中收到此错误:

Uncaught TypeError: stack.map is not a function

有什么问题?我应该如何将 useStack 输出 分配给 const {stack, push, pop} ??

感谢您的帮助。

【问题讨论】:

  • 您的 useStack 返回一个带有键 arrayadddel 的对象。您必须将它们以及函数(假设您继续使用对象简写)重命名为 stackpushpop,以便按照您的方式使用它。
  • PS 你的adddel(或pushpop,如果你将它们重命名为)不会像我想的那样工作。但这是一个不同的问题。
  • @RobinZigmond 为什么名称应该相同?我做到了,现在页面变成空白并给了我:(stack.map 不是函数)
  • 啊,这个新错误是因为我在第二条评论中提到的问题
  • 至于为什么名称应该相同,我不明白这个问题。如果useStack 返回一个对象,其唯一的键是arrayadddel,你为什么期望它有一个stack 属性而不是undefined

标签: javascript reactjs react-hooks


【解决方案1】:

useStack 挂钩存在几个问题:

  • 你的add函数:
const add = (value) => {
    return setArray(array.push(value));
}

array.push returns a number(新数组的长度),不是添加新元素后的数组。这会导致白页(由 JavaScript TypeError 引起,因为现在 stack 不再是数组)。

要解决这个问题,我建议:

const add = (value) => {
    return setArray(array.concat(value));
}
  • 你的del函数:
const del = () => {
    return setArray(array.pop());
}

在这种情况下,array.pop returns the value 刚刚被弹出,所以现在堆栈是一个字符串而不是一个数组。

要解决这个问题,我建议:

const del = () => {
    return setArray(array.slice(1));
}

这是一个工作示例:https://codesandbox.io/s/brave-neumann-41ltx?file=/src/App.js

【讨论】:

  • 非常感谢。这是关于 push & pop 的一个很好的观点。当然 'slice(1)' 应该更改为 'slice(0, array.length-1)',因为我想删除最后一个值。另外,这可能是堆栈和推送和弹出在 {} 内而不是 []? (结果,array & add & del 在 {} 内)。因为在项目中,他们希望我改变 hooks.js。
  • 我找到了答案:我将 'array' & 'add' & 'del' 的名称更改为 'stack' & 'push' & 'pop'。在 components.js 中:“const {stack, push, pop} = useStack();”在 hooks.js 中:“return {stack, push, pop};”。
猜你喜欢
  • 2015-02-14
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 2016-06-29
  • 1970-01-01
  • 2021-01-17
  • 2021-12-03
相关资源
最近更新 更多