【问题标题】:Stencil Lifecycle method not rendering模具生命周期方法不渲染
【发布时间】:2021-05-19 19:37:36
【问题描述】:

我有一个来自 apollo-client 的查询。在我的componentWillLoad() 生命周期方法中,我可以看到查询正在加载(使用console.log)。但我看不到render() 中的数据。我知道这是由于ScopecomponentWillLoad() 功能。但是我无法解决这个问题。我想将数据作为State 进行渲染。

我的代码如下:

import gql from 'graphql-tag';
import getApolloClient from '../../apollo-client';


const BOOK_LIST = gql(`
   query Books($id: ID!) {
      bookbook(id: $id)  {
            name
            id
            year
            }
        }
  `);

@Component({
  tag: 'book-details',
  shadow: false,
})
export class BookDetails {
  batches: any;
  error: boolean;

  async componentWillLoad() {
    
    const apolloClient = getApolloClient();
    const bookList = await apolloClient.query({
      query: BOOK_LIST,
      variables:{"id": "230"}
    });
    console.log(activeBatches.data)
    
  }
  
  render() {
  
    return (
      <Host>
      {bookList}
      </Host>
    );
  }
}



【问题讨论】:

  • 然后将数据存储在组件状态中,而不是存储在局部变量中?
  • 你能详细说明一下吗?

标签: javascript reactjs scope lifecycle stenciljs


【解决方案1】:
 private bookList;

async componentWillLoad() {
    
    const apolloClient = getApolloClient();
    this.bookList = await apolloClient.query({
      query: BOOK_LIST,
      variables:{"id": "230"}
    });
    console.log(activeBatches.data)
    
  }

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 2012-06-13
  • 2020-11-21
  • 1970-01-01
相关资源
最近更新 更多