質問編集履歴

2

書式の改善

2020/04/07 06:23

投稿

bota
bota

スコア22

test CHANGED
File without changes
test CHANGED
@@ -108,6 +108,96 @@
108
108
 
109
109
  https://www.gatsbyjs.org/packages/gatsby-image/#art-directing-multiple-images
110
110
 
111
+ ```jsx
112
+
113
+ import React from "react"
114
+
115
+ import { graphql } from "gatsby"
116
+
117
+ import Img from "gatsby-image"
118
+
119
+
120
+
121
+ export default ({ data }) => {
122
+
123
+ // Set up the array of image data and `media` keys.
124
+
125
+ // You can have as many entries as you'd like.
126
+
127
+ const sources = [
128
+
129
+ data.mobileImage.childImageSharp.fluid,
130
+
131
+ {
132
+
133
+ ...data.desktopImage.childImageSharp.fluid,
134
+
135
+ media: `(min-width: 768px)`,
136
+
137
+ },
138
+
139
+ ]
140
+
141
+
142
+
143
+ return (
144
+
145
+ <div>
146
+
147
+ <h1>Hello art-directed gatsby-image</h1>
148
+
149
+ <Img fluid={sources} />
150
+
151
+ </div>
152
+
153
+ )
154
+
155
+ }
156
+
157
+
158
+
159
+ export const query = graphql`
160
+
161
+ query {
162
+
163
+ mobileImage: file(relativePath: { eq: "blog/avatars/kyle-mathews.jpeg" }) {
164
+
165
+ childImageSharp {
166
+
167
+ fluid(maxWidth: 1000, quality: 100) {
168
+
169
+ ...GatsbyImageSharpFluid
170
+
171
+ }
172
+
173
+ }
174
+
175
+ }
176
+
177
+ desktopImage: file(
178
+
179
+ relativePath: { eq: "blog/avatars/kyle-mathews-desktop.jpeg" }
180
+
181
+ ) {
182
+
183
+ childImageSharp {
184
+
185
+ fluid(maxWidth: 2000, quality: 100) {
186
+
187
+ ...GatsbyImageSharpFluid
188
+
189
+ }
190
+
191
+ }
192
+
193
+ }
194
+
195
+ }
196
+
197
+ `
198
+
199
+ ```
200
+
111
201
  こちらを参考にしながら試行錯誤しているもののなかなかうまくいきません。
112
202
 
113
203
 

1

書式の改善

2020/04/07 06:23

投稿

bota
bota

スコア22

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,103 @@
4
4
 
5
5
  画像の取り扱いについて
6
6
 
7
+ ```ここに言語を入力
8
+
9
+ import React from 'react'
10
+
11
+ import { StaticQuery, graphql } from 'gatsby'
12
+
13
+ import Img from 'gatsby-image'
14
+
15
+
16
+
17
+ // 画像ファイルパスをプロパティに取るようなコンポーネントを定義
18
+
19
+ export default ({ filename }) => (
20
+
21
+
22
+
23
+ // ページじゃないコンポーネントでもGraphQLが使えるように
24
+
25
+ // StaticQueryタグを使う
26
+
27
+ <StaticQuery
28
+
29
+
30
+
31
+ // GraphQLのクエリ引数には何も指定しない!
32
+
33
+ query={graphql`
34
+
35
+ query {
36
+
37
+ images: allFile {
38
+
39
+ edges {
40
+
41
+ node {
42
+
43
+ relativePath
44
+
45
+ name
46
+
47
+ childImageSharp {
48
+
49
+ sizes(maxWidth: 800) {
50
+
51
+ ...GatsbyImageSharpSizes
52
+
53
+ }
54
+
55
+ }
56
+
57
+ }
58
+
59
+ }
60
+
61
+ }
62
+
63
+ }
64
+
65
+ `}
66
+
67
+
68
+
69
+ // 全画像情報がdataに代入されている
70
+
71
+ render={(data) => {
72
+
73
+
74
+
75
+ // 指定した画像ファイルパス(コンポーネントのプロパティ)と
76
+
7
- https://takumon.com/simple-gatsby-image-wrapper
77
+ // 一致するgatsby-image用の情報を取得
78
+
79
+ const image = data.images.edges.find(n => {
80
+
81
+ return n.node.relativePath.includes(filename)
82
+
83
+ })
84
+
85
+
86
+
87
+ if (!image) return
88
+
89
+
90
+
91
+ // Imgタグでgatsby-imageで最適化された画像を表示する
92
+
93
+ const imageSizes = image.node.childImageSharp.sizes
94
+
95
+ return <Img sizes={imageSizes} />
96
+
97
+ }}
98
+
99
+ />
100
+
101
+ )
102
+
103
+ ```
8
104
 
9
105
  上記を参考に、パスを渡せばgatsby-imageで画像を表示はできるのですが、
10
106