質問するログイン新規登録

質問編集履歴

1

回答の内容に修正したコードを追記しました

2019/08/24 06:45

投稿

wisheebell
wisheebell

スコア7

title CHANGED
File without changes
body CHANGED
@@ -69,4 +69,122 @@
69
69
  ### 補足情報(FW/ツールのバージョンなど)
70
70
 
71
71
  cloud9
72
- React.js v16.8.6
72
+ React.js v16.8.6
73
+
74
+ ###追記
75
+ 回答のコメントより、改善したものを追記します。
76
+ ```Index
77
+ import React, { Component } from 'react'
78
+ import { withRouter } from 'react-router';
79
+ import axios from 'axios';
80
+ import Typography from '@material-ui/core/Typography'
81
+ import Post from '../component/Post';
82
+
83
+ class Index extends Component {
84
+ constructor(props) {
85
+ super(props)
86
+ this.state = {
87
+ posts: []
88
+ }
89
+ }
90
+
91
+
92
+ componentDidMount() {
93
+ axios
94
+ .get('https://6ccf1b5402214ffdb31120bbd0a35c58.vfs.cloud9.us-east-2.amazonaws.com/api/v1/posts')
95
+ .then((results) => {
96
+ console.log(results)
97
+ this.setState({ posts: results.data })
98
+ })
99
+ .catch((data) =>{
100
+ console.log(data)
101
+ })
102
+ }
103
+
104
+ render(){
105
+ return(
106
+ <div>
107
+ {(() => {
108
+ if (this.state.posts.length > 0) {
109
+ return (
110
+ this.state.posts.map( (post, i) => {
111
+ return(
112
+ <Post
113
+ key={i}
114
+ createdAt={post.created_at}
115
+ name='testUser'
116
+ title={post.title}
117
+ content={post.content}
118
+ />
119
+ )
120
+ })
121
+ )
122
+ } else {
123
+ return (
124
+ <Typography variant="h6" component="h2">
125
+ まだなにも投稿されていません。
126
+ </Typography>
127
+ )
128
+ }
129
+ })()}
130
+ </div>
131
+ );
132
+ }
133
+ }
134
+
135
+ export default Index;
136
+ ```
137
+ ```Post
138
+ import React, { Component } from 'react';
139
+ import { makeStyles, withStyles } from '@material-ui/core/styles';
140
+ import Card from '@material-ui/core/Card';
141
+ import CardActions from '@material-ui/core/CardActions';
142
+ import CardContent from '@material-ui/core/CardContent';
143
+ import Button from '@material-ui/core/Button';
144
+ import Typography from '@material-ui/core/Typography';
145
+
146
+ class Post extends Component {
147
+ render(){
148
+ const classes = makeStyles({
149
+ card: {
150
+ minWidth: 275,
151
+ },
152
+ bullet: {
153
+ display: 'inline-block',
154
+ margin: '0 2px',
155
+ transform: 'scale(0.8)',
156
+ },
157
+ title: {
158
+ fontSize: 14,
159
+ },
160
+ pos: {
161
+ marginBottom: 12,
162
+ },
163
+ });
164
+
165
+ return (
166
+ <Card className={classes.card}>
167
+ <CardContent>
168
+ <Typography className={classes.title} color="textSecondary" gutterBottom>
169
+ {this.props.createdAt}
170
+ </Typography>
171
+ <Typography variant="h5" component="h2">
172
+ {this.props.name}
173
+ </Typography>
174
+ <Typography className={classes.pos} color="textSecondary">
175
+ {this.props.title}
176
+ </Typography>
177
+ <Typography variant="body2" component="p">
178
+ {this.props.content}
179
+ </Typography>
180
+ </CardContent>
181
+ <CardActions>
182
+ <Button size="small">Learn More...</Button>
183
+ </CardActions>
184
+ </Card>
185
+ );
186
+ }
187
+ }
188
+
189
+ export default Post;
190
+ ```