<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Suliman-A</title>
    <description>The latest articles on DEV Community by Suliman-A (@suliman).</description>
    <link>https://dev.to/suliman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F603864%2F871f6ccc-aa4d-4df2-98c8-3191277adac3.jpeg</url>
      <title>DEV Community: Suliman-A</title>
      <link>https://dev.to/suliman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suliman"/>
    <language>en</language>
    <item>
      <title>Type 'boolean' is not assignable to type (react.js and TypeScript) error</title>
      <dc:creator>Suliman-A</dc:creator>
      <pubDate>Sat, 24 Jul 2021 10:41:06 +0000</pubDate>
      <link>https://dev.to/suliman/type-boolean-is-not-assignable-to-type-react-js-and-typescript-error-5h5</link>
      <guid>https://dev.to/suliman/type-boolean-is-not-assignable-to-type-react-js-and-typescript-error-5h5</guid>
      <description>&lt;p&gt;im new to react and typeScript. i created a custom hook to fetch an API and i want to pass my data to another component but for some reason im getting the following typeScript error.&lt;/p&gt;

&lt;p&gt;pls suggest where im doing it wrong&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:/Users/63906/Documents/Web Development/citcs-blogs/src/components/Home.tsx
TypeScript error in C:/Users/63906/Documents/Web Development/citcs-blogs/src/components/Home.tsx(16,26):
Type 'true | IBlog[]' is not assignable to type '{ title: string; body: string; author: string; id: number; url: string; }[]'.
  Type 'boolean' is not assignable to type '{ title: string; body: string; author: string; id: number; url: string; }[]'.  TS2322

    14 |       {error &amp;amp;&amp;amp; &amp;lt;div&amp;gt;{error}&amp;lt;/div&amp;gt;}
    15 |       {isPending &amp;amp;&amp;amp; &amp;lt;div&amp;gt;loading...&amp;lt;/div&amp;gt;}
  &amp;gt; 16 |       {data &amp;amp;&amp;amp; &amp;lt;BlogList data={data} /&amp;gt;}
       |                          ^
    17 |     &amp;lt;/div&amp;gt;
    18 |     )
    19 | }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is my custome hook to call the API&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";

export interface IBlog {
    data: {
        title: string;
        body: string;
        author: string;
        id: number
        url: string;
    }[]
}
const useFetch = (url: string) =&amp;gt; { // url: string

    const [data, setData] = useState&amp;lt;IBlog['data']&amp;gt;([]);
    const [isPending, setIsPending] = useState(false);
    const [error, setError] = useState(null);

    useEffect(() =&amp;gt; {
        const abortController = new AbortController();


        setTimeout(() =&amp;gt; {
            fetch(url, { signal: abortController.signal })
                .then(res =&amp;gt; {
                    if (!res.ok) { // error coming back from server
                        throw Error('could not fetch the data for that resource');
                    }
                    return res.json();
                })
                .then(data =&amp;gt; {
                    setIsPending(false);
                    setData(data);
                    setError(null);
                })
                .catch(err =&amp;gt; {
                    if (err.name === "AbortError") {
                        console.log('fech aborted');
                    } else {
                        // auto catches network / connection error
                        setIsPending(false);
                        setError(err.message);
                    }
                })
        }, 1000);

        // cleanup funtion
        return () =&amp;gt; abortController.abort();

    }, [url])

    return [data, isPending, error]
}

export default useFetch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and this the home component which where i want to read my data and getting my error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import BlogList from "./BlogList";
import useFetch from "./useFetch";
import React, { FC } from 'react';
import { IBlog } from "./useFetch"

const Home: FC = () =&amp;gt; {

    const [data, isPending, error] = useFetch("http://localhost:8000/blogs")

    console.log(data, isPending, error, "from hook")

    return (
        &amp;lt;div className='home'&amp;gt;
            {error &amp;amp;&amp;amp; &amp;lt;div&amp;gt;{error}&amp;lt;/div&amp;gt;}
            {isPending &amp;amp;&amp;amp; &amp;lt;div&amp;gt;loading...&amp;lt;/div&amp;gt;}
            {data &amp;amp;&amp;amp; &amp;lt;BlogList data={data} /&amp;gt;}
        &amp;lt;/div&amp;gt;
    )
}

export default Home
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and this is the BlogList component to loop and display my data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// import { Link } from "react-router-dom";
 import { IBlog as IProp } from "./useFetch"


const BlogList: React.FC&amp;lt;IProp&amp;gt; = ({ data }: IProp) =&amp;gt; {

    const renderList = () =&amp;gt; {
    return data.map((blogIndex) =&amp;gt; {
        return (
                &amp;lt;div className='blog-preview' key={blogIndex.id}&amp;gt;
                    &amp;lt;a href={`/blogs/${blogIndex.id}`}&amp;gt;
                        &amp;lt;h2&amp;gt;{blogIndex.title}&amp;lt;/h2&amp;gt;
                        &amp;lt;p&amp;gt;Written by {blogIndex.author}&amp;lt;/p&amp;gt;
                    &amp;lt;/a&amp;gt;

                &amp;lt;/div&amp;gt;
            )

        })

    }
    return (
        &amp;lt;ul&amp;gt;
            {renderList()} 
        &amp;lt;/ul&amp;gt;
    )

}

export default BlogList;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
