質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

1回答

275閲覧

componentDidMountとrender()内の変数のやりとり

eng___

総合スコア5

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2019/10/12 07:45

前提・実現したいこと

render内の下記の箇所で、マークダウンファイルを読み取って、変数:numberOfAdsを取得しています。

const { post2, numberOfAds } = insertAdsense(post.html, slug)

この後に、componentDidMount内で変数:numberOfAdsを使用したいのですが、どうすればよいでしょうか。

発生している問題・エラーメッセージ

class BlogPostTemplate extends React.Component { componentDidMount() { if (window) for (let i = 0; i < numberOfAds; i++) { (window.adsbygoogle = window.adsbygoogle || []).push({}); } } render() { const post = this.props.data.markdownRemark const siteTitle = this.props.data.site.siteMetadata.title const { relatedPosts, slug } = this.props.pageContext const { post2, numberOfAds } = insertAdsense(post.html, slug) const { title, description, date, category, tags } = post.frontmatter const sizes = post.frontmatter.featuredImage ? post.frontmatter.featuredImage.childImageSharp.sizes : 0 const { categories } = this.props.data.site.siteMetadata const categoryObject = categories.find(cat => { return cat.slug === category }) const categoryColor = categoryObject ? categoryObject.color : "#555" return ( <Layout location={this.props.location} title={siteTitle} pageAd={"true"}> <SEO title={title} description={description || post.excerpt} /> <Helmet> <link rel="canonical" href={`https://englissu.com${this.props.location.pathname}`} /> </Helmet> <PostJsonLd title={title} description={description || post.excerpt} date={date} url={this.props.location.href} categorySlug={category} /> <Content> <ContentMain> <PostDate>{date}</PostDate> <PostTitle>{title}</PostTitle> <Labels> <CategoryLabel slug={category} isLink="true" /> {tags.map(({ tag }, index) => { return <TagLabel key={index} slug={tags[index]} isLink="true" /> })} </Labels> <FeaturedImage sizes={sizes} className="featured-image" /> <PostContent categoryColor={categoryColor} categorySlug={category} dangerouslySetInnerHTML={{ __html: post2 }} /> <FollowBudge /> <ShareButtons slug={slug} title={title} /> </ContentMain> <aside id="aside"> <RelatedPosts posts={relatedPosts} /> </aside> </Content> </Layout> ) } } export default BlogPostTemplate export const pageQuery = graphql` query blogPostBySlug($slug: String!) { site { siteMetadata { title author categories { name slug color } } } markdownRemark(fields: {slug: {eq: $slug } }) { id excerpt(pruneLength: 160) html frontmatter { title description date(formatString: "YYYY.MM.DD") category tags featuredImage { childImageSharp { sizes(maxWidth: 630) { ...GatsbyImageSharpSizes } } } } } } `

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

render内の下記の箇所で、マークダウンファイルを読み取って、変数:numberOfAdsを取得しています。

render()の際にこの処理を毎回行う必要があるのなら別ですが、そうでないならコンストラクタにうつしてしまったほうがいいかもしれません。

投稿2019/10/12 08:27

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

eng___

2019/10/12 12:04

おっしゃる通り、constructorにうつせばいい問題でした???? 早急のコメントありがとうございます!助かりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問