【发布时间】:2022-11-16 06:46:44
【问题描述】:
来自我的 Hooks“useStore.tsx”的完整代码,是根据教程构建的。除了“removeCube”之外,这里的一切都完美无缺。没有错误,它只是没有删除多维数据集。任何提示将不胜感激!
完整代码在这里:https://github.com/He-fast-us/Tunnel-Art
`
import create from 'zustand';
import { nanoid } from 'nanoid';
import { Triplet } from '@react-three/cannon';
import * as textures from '../Images/textures';
import { useState } from 'react';
const getLocalStorage = (key: string) => JSON.parse(window.localStorage.getItem(key)!)
const setLocalStorage = (key: string, value: unknown) => (window.localStorage.setItem(key, JSON.stringify(value)))
type TextureId = keyof typeof textures
type State = {
texture: TextureId,
cubes: Array<{ key: string, pos: Triplet, texture: TextureId }>,
addCube: (x: number, y: number, z: number) => void,
removeCube: (x: number, y: number, z: number) => void,
saveWorld: () => void,
resetWorld: () => void,
setTexture: (texture: TextureId) => void,
}
export const useStore = create<State>((set) => ({
texture: 'dirtTexture',
cubes: getLocalStorage('cubes') || [],
addCube: (x, y, z) => {
set((prev) => ({
cubes: [
...prev.cubes,
{
key: nanoid(),
pos: [x, y, z],
texture: prev.texture
}
]
}))
},
removeCube: (x, y, z) => {
set((prev) => ({
cubes: prev.cubes.filter(cube => {
const [X, Y, Z] = cube.pos
return X !== x || Y !== y || Z !== z
})
}))
},
setTexture: (texture) => {
set(() => ({
texture
}))
},
saveWorld: () => {
set((prev) => {
setLocalStorage('cubes', prev.cubes)
})
},
resetWorld: () => {
set(() => ({
cubes: []
}))
},
}))
`
我检查了拼写错误并确认添加和删除匹配的语法,但我正在努力查看这里有什么不起作用
【问题讨论】:
标签: reactjs typescript redux react-hooks state