javascript - 如何用 const 声明对象属性
问题描述
我需要为此声明一个变量formstate.title
:
const initialState = { description: '', place: "", date: "", membersJoinedCounter: 0, title: "" };
const [formState, setFormState] = useState(initialState);
function setInput(key, value,) {
setFormState({ ...formState, [key]: value })
}
const userTitle = await API.graphql(graphqlOperation(getUserTitle, {id: userInfo.attributes.sub}))
formState.title = userTitle.data.getUser.title;
如何做到这一点?
解决方案
不清楚你想问什么,但假设你想在获取数据后设置标题值,你可以通过使用 useState 钩子提供的 setFormstate 方法来实现(你不能直接设置值,因为 useState 钩子返回的 formstate 是不可变的)
替换代码中的以下行
formState.title = userTitle.data.getUser.title
低于一个
setFormState(prev=>({...prev,title:userTitle.data.getUser.title}))
推荐阅读
- docker - GKE not able to reach MongoDB Atlas
- python - 当在执行 SVM 时预期一维数组出错时,传递了列向量 y?
- django - 在 Django 中创建计划
- c# - 如果给定的用户输入包含在该项目中,C# 返回搜索结果(项目)
- common-lisp - clsql 连接池
- azure - Azure Function 在部署后偶尔仍会运行旧代码
- c - LZ77优化
- python - 如何使用给定列表的for循环在Python中反转索引值
- java - Android中的媒体播放器通知
- javascript - 对 graphql 查询使用 try catch 块