Swix
Swix

Reputation: 2123

React Hooks and TypeScript Fetching API: Object is possibly 'null'

I'm doing a tutorial on React Hooks and fetching data. Here's my component to fetch customers and map them out in a list:

const useFetch = (url: string) => {
  const [customers, setCustomers] = useState<null | []>(null);
  const [loading, setLoading] = useState(true);

  // Similiar to componentDidMount() and componentDidUpdate()
  useEffect(() => {
    const fetchData = async () => {
      const result = await axios(url);
      setCustomers(result.data);
      setLoading(false);
    };
    fetchData();
  });

  return { customers, loading };
};

const url = 'https://jsonplaceholder.typicode.com/users';

export const LatestCustomers: React.FC<Props> = ({
  description
}: Props): JSX.Element => {
  const { customers, loading } = useFetch(url);

  return (
    <Container>
      {loading ? (
        <div>...Loading...</div>
      ) : (
        <tr>
          {customers.map(customer => (
            <div key="user.id">{customer.name}</div>
          ))}
        </tr>
      )}
    </Container>
  );
};

With that, I got the error:

Object is possibly 'null'.  TS2531

    108 |               ) : (
    109 |                 <tr>
  > 110 |                   {customers.map(customer => (
        |                    ^
    111 |                     <div key="user.id">{customer.name}</div>
    112 |                   ))}
    113 |                 </tr>

How do I solve this?

Upvotes: 1

Views: 2473

Answers (2)

GibboK
GibboK

Reputation: 73988

A robust solution to handle nullable value could be to use an Option in fp-ts and fromNullable

https://github.com/gcanti/fp-ts/blob/master/src/Option.ts

Example:

{fromNullable(customers).map(x=> {...})}

Interesting article: https://davetayls.me/blog/2018/05/20/fp-ts-01-working-with-nullable-values

Otherwise more straightforward approach:

{customers && customers.map(x => ( ...

Upvotes: 1

djskinner
djskinner

Reputation: 8125

Because the type supplied to useState is null | [] then customers is given that type signature.

There are a couple of ways to deal with this. My suggestion would be to start with an empty array:

const [customers, setCustomers] = useState<[]>([]);

Alternatively if you wish to maintain the optional typing then you should check customers is not null first:

{customers && customers.map(customer => ( ...

Or if you're really sure it will always be defined you could use the TypeScript non-null assertion ! operator:

{customers!.map(customer => (

Upvotes: 1

Related Questions