如何解决此“重新渲染过多”错误?
我正在使用 try、catch 方法、useState、setState 反应钩子。
我正在尝试从 api 获取数据并在网络上打印。
此处发生错误:setEmoticonThm(newEmoticonThms)
错误:重新渲染过多。React 限制了渲染的数量以防止无限循环。
const [emoticonThm, setEmoticonThm] = useState([]);
const getToken = async () => {
try {
const emoticon = await axios.get(`${process.env.EMOTICON_ENDPOINT}`, {
headers: {
Authorization: accessToken
}
})
let newEmoticonThms = []
emoticon.data.emoticonPacks.map( (emoticon) => {
newEmoticonThms.push({
id: emoticon.id,
image:url + emoticon.image
})
})
setEmoticonThm(newEmoticonThms)
} catch (err) {
console.log(err)
}
}
const onClickSticker = () => {
getToken()
handleKeyboardOpen()
}
return (
...
<Sticker onClick={onClickSticker}/>
<TabContainer>
{emoticonThm.map((emoticon, index) => {
return (
<EmoticonThmButton
key={index}
onClick={setSelectedThm(index)}
>
<EmoticonThmImage
key={index}
onClick={onEmoticonClick}
src={img}
/>
</EmoticonThmButton>
)
})}
</TabContainer>
)
我添加了我的代码。我怎样才能正确?TT