回答編集履歴
3
a
test
CHANGED
@@ -27,3 +27,193 @@
|
|
27
27
|
}}/>
|
28
28
|
|
29
29
|
```
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
# 即席デモコード
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
## デモ
|
38
|
+
|
39
|
+
![イメージ説明](7b26063084b16bb5d0c34ad62118b0af.gif)
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
## index.js
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
import React from 'react';
|
48
|
+
|
49
|
+
import { render } from 'react-dom';
|
50
|
+
|
51
|
+
import { BrowserRouter, Route, Switch, Link } from 'react-router-dom';
|
52
|
+
|
53
|
+
import './styles.css';
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
const Detail = ({ location }) => {
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
return (
|
62
|
+
|
63
|
+
<div className={location.state.className}>Detail</div>
|
64
|
+
|
65
|
+
)
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
class UserList extends React.Component {
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
constructor(props) {
|
76
|
+
|
77
|
+
super(props);
|
78
|
+
|
79
|
+
this.state = {
|
80
|
+
|
81
|
+
users: [
|
82
|
+
|
83
|
+
{ id: 1, name: 'a' }, //1
|
84
|
+
|
85
|
+
{ id: 2, name: 'b' }, //2
|
86
|
+
|
87
|
+
{ id: 3, name: 'c' }, //3
|
88
|
+
|
89
|
+
{ id: 4, name: 'd' }, //1
|
90
|
+
|
91
|
+
{ id: 5, name: 'e' }, //2
|
92
|
+
|
93
|
+
{ id: 6, name: 'f' } //3
|
94
|
+
|
95
|
+
]
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
render() {
|
104
|
+
|
105
|
+
return (
|
106
|
+
|
107
|
+
<ul>
|
108
|
+
|
109
|
+
{
|
110
|
+
|
111
|
+
this.state.users.map((user, index) => {
|
112
|
+
|
113
|
+
return (
|
114
|
+
|
115
|
+
<li key={user.id}>
|
116
|
+
|
117
|
+
<Link to={{
|
118
|
+
|
119
|
+
pathname: `detail/${user.id}`,
|
120
|
+
|
121
|
+
state: { className: `detail${index % 3 + 1}` }
|
122
|
+
|
123
|
+
}}>{user.name}</Link>
|
124
|
+
|
125
|
+
</li>
|
126
|
+
|
127
|
+
)
|
128
|
+
|
129
|
+
})
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
</ul>
|
134
|
+
|
135
|
+
)
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
render(
|
146
|
+
|
147
|
+
<BrowserRouter>
|
148
|
+
|
149
|
+
<Switch>
|
150
|
+
|
151
|
+
<Route path='/' exact component={UserList} />
|
152
|
+
|
153
|
+
<Route path='/detail/:id' component={Detail} />
|
154
|
+
|
155
|
+
</Switch>
|
156
|
+
|
157
|
+
</BrowserRouter>,
|
158
|
+
|
159
|
+
document.getElementById('root')
|
160
|
+
|
161
|
+
);
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
## styles.css
|
170
|
+
|
171
|
+
```
|
172
|
+
|
173
|
+
ul li:nth-child(3n+1) {
|
174
|
+
|
175
|
+
background-color: #949CF6;
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
ul li:nth-child(3n+2) {
|
182
|
+
|
183
|
+
background-color: #F499A7;
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
ul li:nth-child(3n+3) {
|
190
|
+
|
191
|
+
background-color: #FFD89C;
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
.detail1 {
|
198
|
+
|
199
|
+
background-color: #949CF6;
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
.detail2 {
|
206
|
+
|
207
|
+
background-color: #F499A7;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
.detail3 {
|
214
|
+
|
215
|
+
background-color: #FFD89C;
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
```
|
2
a
test
CHANGED
@@ -10,13 +10,19 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
+
[https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/Link.md#to-object](https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/Link.md#to-object)
|
14
|
+
|
13
15
|
```
|
14
16
|
|
15
17
|
<Link to={{
|
16
18
|
|
17
|
-
pathname: '/
|
19
|
+
pathname: '/courses',
|
18
20
|
|
21
|
+
search: '?sort=name',
|
22
|
+
|
23
|
+
hash: '#the-hash',
|
24
|
+
|
19
|
-
state: { ba
|
25
|
+
state: { fromDashboard: true }
|
20
26
|
|
21
27
|
}}/>
|
22
28
|
|
1
a
test
CHANGED
@@ -2,4 +2,22 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
その場合であれば、単純にクリックされたリストの背景色、または背景色が定義されたクラス名をDetailコンポーネントのpropsで渡してあげて、Detailコンポーネントは渡された背景色、または、クラス名を使ってレンダーしてあげれば良いだけだと思います。
|
5
|
+
その場合であれば、単純にクリックされたリストの背景色、または背景色が定義されたクラス名をDetailコンポーネントのpropsで渡してあげて、Detailコンポーネントは渡された背景色、または、クラス名を使って自身をレンダーしてあげれば良いだけだと思います。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
react-routerを使っているのであれば、Linkコンポーネントにはオブジェクトを渡せるので、その中のstateプロパティーで色なりクラス名を渡してあげて、遷移先の詳細画面コンポーネントに渡ってくる値を取り出してあげると良いと思います。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```
|
14
|
+
|
15
|
+
<Link to={{
|
16
|
+
|
17
|
+
pathname: '/detail',
|
18
|
+
|
19
|
+
state: { background: '#000000 }
|
20
|
+
|
21
|
+
}}/>
|
22
|
+
|
23
|
+
```
|