javascript - 如何使用反应钩子动态创建状态对象?
问题描述
到目前为止,在我的代码中,我有一个对象可以代表页面上图像的所有数据
this.state = {
img-1: {
x: 0,
y: 0,
rotation: 0
},
img-2: {
x: 20,
y: 200,
rotation: 50
}
}
每次对象收到一个新的孩子时,它都会img-id
向每次<img id=${id} update={this.update} />
更新的状态添加一个新的状态。
将计算坐标或旋转等功能移动到他们自己的自定义钩子中将大大改善我的代码在可维护性和测试方面,但我真的没有看到将所有这些数据存储在带有钩子的集中对象中的好方法。
据我了解,我必须设置一个新的
[img-1, setImg-1] = useState({ x: 0, y:0, rotation: 0 })
对于每个孩子,据我所知,这是不可能的,因为挂钩必须在顶层声明或设置一个非常深的对象,更新起来有点笨拙:
[images, setImages] = useState({
img-1: {
x: 0,
y: 0,
rotation: 0
}
})
const createImg = (newImg) => { setImages({...images, newImg}) }
const updateImg = (id, updatedImg) => {
setImages({ ...images, [`img-${id}`]{...updatedImg} }
)}
有没有更清洁/更易读的方法,还是我只需要求助于将所有内容嵌套在一个对象中?
解决方案
代替使用 a useState
,您可以更好地利用useReducer
和控制您的状态并处理状态的动态添加
const initialState = {
img-1: {
x: 0,
y: 0,
rotation: 0
}
}
const reducer= (state, action) =>{
switch(action.type) {
'ADD_IMAGE': return {
...state,
[action.itemkey]: action.payload
}
'UPDATE_IMAGE: return {
...state,
[action.id]: {...state[action.id], ...action.payload}
}
default: {
return state;
}
}
}
在功能组件中
const [state, dispatch] = useReducer(reducer, initialState);
const createImg = (newImg) => { dispatch({ type: 'ADD_IMAGE', payload: {newImg}, itemKey: `item-${Object.keys(state).length + 1}`
const updateImg = (id, updatedImg) => {
dispatch({type: 'UPDATE_IMAGE', id, payload: updatedImg })
)}
推荐阅读
- google-apps-script - 从列表中翻译 Google Doc - 问题:在某些文档上无法完全运行
- c++ - 奇怪的 g++ 错误,带有关于左值和赋值的简单代码
- mysql - Setting rank based on query
- nginx - 允许不在使用 10.0.0.1 的新路由器上工作
- c++ - “深度”模板参数推导可行吗?
- c - 计数排序显示奇怪的行为
- java - 如何编译存储在字符串中的 Java 类,或者使用给定输入的路径?
- r - 无法在 r studio 中安装
- java - 布尔值已更改但 if 语句无法识别
- python - 从 Instagram 中提取关注者数量