javascript - setState 不会更新 react 中的状态

标签 javascript reactjs react-component

最佳答案

您不需要刷新状态

亮点: 在状态中添加响应并使用状态来渲染卡片

尝试下面的代码:

state = {
  reminders: []
}

fetch('http://localhost:5000/addrem',{
            method: 'POST',
            headers:{'Content-Type': 'application/json'},
            body: JSONdata,
        }).then(resp => resp.json().then(data =>{
            if (data){
              this.response = data
              this.setState({
                reminders: this.response.reminders
              })
            }
          }))

{
  this.state.reminders.map((item,i=0) =>{
      return <Card key={i++} reminder={item}/>
  })
}

P.S - 不要使用 map 迭代(i)作为键,使用提醒数组中唯一标识该卡的属性。

关于javascript - setState 不会更新 react 中的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68540711/

相关文章:

javascript - 如何使用 componentWillUnmount 删除 React.js 中的 setInterval

reactjs - 在 React 中制作包装器组件

javascript - 如何使用 Typescript 将对象传递给 ReactJS 中的自定义子组件?

javascript - 使用 CSS 根据 <tr> 在 HTML 中出现的次数更改 <tr> bgcolor

javascript - 嵌套循环迭代

css - 覆盖样式组件中子样式的最佳方法

reactjs - React/Material UI - 防止在弹出框上滚动 body

javascript - react 路由器 : Active Link not matching the URL

javascript - 在javascript中按回车键时登录?

javascript - jQuery.on() 在 WordPress 管理面板中不起作用