The full error:
This expression is not callable. Not all constituents of type 'boolean | (() => void)' are callable. Type 'false' has no call signatures. TS2349
This is the code that was triggering the error:
export const useLoading = (defaultValue: boolean = false) => {
const [loading, setLoading ] = useState(defaultValue)
const startLoading = () => setLoading(true)
const stopLoading = () => setLoading(false)
return [
loading,
startLoading,
stopLoading
]
}
CAUSE: the return of the array. Having arrays without a specific type gave me this error as wel: Line 0: Parsing error: : Cannot read property ‘map’ of undefined. The solution was to typehint the array.
SOLUTION: don’t return the array just like that, add ‘as const
` at the end of that return statement.
export const useLoading = (defaultValue: boolean = false) => {
const [loading, setLoading ] = useState(defaultValue)
const startLoading = () => setLoading(true)
const stopLoading = () => setLoading(false)
return [
loading,
startLoading,
stopLoading
] as const
}
This is the post that showed me where to find it: https://github.com/microsoft/TypeScript/issues/35423
If you’re using React and typescript, try these few actions:
rm -rf node_modules; yarn install
build
directory: remove the build
directory and restart the node serviceThis failed:
// don't do this - a 'bare' array as type
const customers: [] = []
SOLUTION: It was fixed by:
// add a type to the array
const customers: string[] = []
It’s nice to have good looking messages in your app. You can have this too with react-toastify.
To have these toast messages in your app, do the following:
yarn add react-toastify
Then, in your toplevel file (sth like index.js or App.js, etc.);
import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
function App(){
const notify = () => toast("Wow so easy !");
return (
<div>
<button onClick={notify}>Notify !</button>
<ToastContainer />
</div>
);
}
If you have an endpoint in your API which delivers a file like text/csv; charset=UTF-8
and you want to let your user download this file, read on.