【问题标题】:How to do realtime fetching in react native?如何在本机反应中进行实时获取?
【发布时间】:2019-07-29 05:42:45
【问题描述】:

嗨,在我的项目中有一个视频的评论部分。所以我期望在用户发布后立即显示 cmets。我尝试了componentWillUpdate() 方法。但它每次都重新渲染 UI ,所以会在 UI 中引起一些震动。此外,我尝试在 postComment 的承诺中调用 fetch 方法。这意味着在发布评论的获取中,我试图调用下一个获取以显示提交的内容cmets.But没有用。以下是我尝试过的代码

代码

postComment(){
fetch(GLOBAL.COMMENT + `${this.props.id}/new/0`, {
        method: 'POST',
        headers: {
            'Authorization': token,
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            comment_content: this.state.text

        })
    })
    .then((response) => response.json())
    .then((responseData) => {

        this.setState({
            text: '',
            isLoad: false
        })
    }).then(() => {
        fetch(GLOBAL.GET_COMMENT + `${this.state.unit_id}/`, {
                method: 'GET',
                headers: {
                    'Authorization': token
                }
            }).then((response) => response.json())
            .then((responseData) => {
                this.setState({
                    comment: responseData.data,
                    tab2: true,
                    tab3: false,
                    isLoading: false

                })
            });
    })

}

有什么解决办法吗?请帮助。任何帮助表示赞赏。谢谢!

【问题讨论】:

  • 您需要做的是在添加评论后更改状态,以便在添加评论后自动重新呈现并显示所有 cmets。所以如果cmets在后端,请求完成后,调用带来所有cmets的函数,更新状态。
  • 您可以将新发布的评论添加到您的平面列表data 而无需获取。无需再次获取
  • @squeekyDave 那么我需要对现有代码进行哪些更改?请帮忙。
  • @anu 所以我假设您将后端的 cmets 存储在数据库中?如果是这种情况,那么在发表评论请求完成后,调用一个函数,该函数将获取所有 cmets 并使用新获取的 cmets 设置状态,这将重新渲染,您将尽快看到新的 cmets它们已发布。
  • @squeekyDave 因此,我必须做类似((then) => getComments() ) 之类的事情,而不是在承诺范围内进行这种提取,我是对的吗?

标签: javascript reactjs react-native fetch real-time


【解决方案1】:

所以它看起来像这样,我不确定你的代码的其余部分是如何设置的,所以请稍加注意。

getComments() {

  fetch(GLOBAL.GET_COMMENT + `${this.state.unit_id}/`, {
                method: 'GET',
                headers: {
                    'Authorization': token
                }
            })
            .then((response) => response.json())
            .then((responseData) => {
                this.setState({
                    comment: responseData.data,
                    tab2: true,
                    tab3: false,
                    isLoading: false

                })
            });
    })

};

postComment() {
  fetch(GLOBAL.COMMENT + `${this.props.id}/new/0`, {
          method: 'POST',
          headers: {
              'Authorization': token,
              'Accept': 'application/json',
              'Content-Type': 'application/json'
          },
          body: JSON.stringify({
              comment_content: this.state.text

          })
      })
      .then((response) => response.json())
      .then((responseData) => {

          this.getComments();
      })

}

【讨论】:

    猜你喜欢
    • 2018-08-15
    • 1970-01-01
    • 2020-08-30
    • 2019-06-12
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多