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

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

新規登録して質問してみよう
ただいま回答率
85.46%
非同期処理

非同期処理とは一部のコードを別々のスレッドで実行させる手法です。アプリケーションのパフォーマンスを向上させる目的でこの手法を用います。

React.js

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

Q&A

2回答

913閲覧

TypeError: resources.map is not a functionというエラーを解消したい

rl0t_ryunosuke

総合スコア14

非同期処理

非同期処理とは一部のコードを別々のスレッドで実行させる手法です。アプリケーションのパフォーマンスを向上させる目的でこの手法を用います。

React.js

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

0グッド

0クリップ

投稿2021/02/18 18:37

前提・実現したいこと

axiosを使用して非同期でデータを取ってきて、羅列したい

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

TypeError: resources.map is not a function

該当のソースコード

ConnectingToAPIコンポーネント

js

1import React, { useState } from "react"; 2 3import { Button } from "../core/Button/Button"; 4import { Resources } from "../core/Resources/Resources"; 5import jsonplaceholder from "../../apis/jsonplaceholder"; 6 7export const ConnectingToApi = () => { 8 const [resources, setResources] = useState([]); 9 10 const getPosts = async () => { 11 try { 12 const posts = await jsonplaceholder.get("/posts"); 13 setResources(posts.data); 14 } catch (err) { 15 console.log(err); 16 } 17 }; 18 19 const getAlbums = async () => { 20 try { 21 const albums = await jsonplaceholder.get("/albums"); 22 setResources(albums.data); 23 } catch (err) { 24 console.log(err); 25 } 26 }; 27 28 return ( 29 <> 30 <Button onClick={getPosts} value="posts" /> 31 <Button onClick={getAlbums} value="albums" /> 32 <Resources resources={resources} /> 33 </> 34 ); 35}; 36

Resourcesコンポーネント

js

1import React from "react"; 2 3export const Resources = (resources) => { 4 console.log(resources); 5 return ( 6 <> 7 {resources.map((resource) => ( 8 <p key={resource.id}>{resource.title}</p> 9 ))} 10 </> 11 ); 12};

BUTTONコンポーネント

js

1import React from "react"; 2import styled, { css } from "styled-components"; 3 4export const Button = ({ onClick, value, isDisabled }) => { 5 return ( 6 <> 7 <StyledButton isDisabled={isDisabled} onClick={onClick}> 8 {value} 9 </StyledButton> 10 </> 11 ); 12}; 13 14const StyledButton = styled.button` 15 ${({ isDisabled }) => 16 isDisabled && 17 css` 18 opacity: 0.3; 19 pointer-events: none; 20 `} 21`; 22

試したこと

console.logでresoucesの内容を確認arrayになっている_proto_もあり。以下画像添付

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答2

0

以下で解決するかと

import React from "react"; export const Resources = (props) => { const { resources } = props return ( <> {resources.map((resource) => ( <p key={resource.id}>{resource.title}</p> ))} </> ); };

投稿2021/02/19 14:04

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

Reactコンポーネントに渡すpropは、{resources: [....]}のようなオブジェクトとして渡されます。

Resources = ({resources}) =>のように引数を分割代入してしまうのが手っ取り早いです。

投稿2021/02/18 22:39

maisumakun

総合スコア145208

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問