Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Passing a react-native useState argument to a component within jest testing environment

$
0
0

I have a modal that takes in an useState argument that is used to show or close the modal based on a button click. (close button is in the modal and show button is in a separate functional component).

When I try to use useState in jest I get the following error:

Invalid hook call. Hooks can only be called inside of the body of a function component

This is how my modal is defined:

export default function PDFViewer({modalVisible, setModalVisible, file, filename}: {modalVisible:boolean, setModalVisible:Dispatch<SetStateAction<boolean>>, file:string, filename?:string }): JSX.Element { ... Modal code ...}

Instead of using use state I am passing in a void function right now but that isn't very useful.

jest render:

        const { getByText} = render(<AuthProvider><PDFViewer modalVisible={false} setModalVisible={function (value: React.SetStateAction<boolean>): void {                    throw new Error("Function not implemented.");                } } file={""} /></AuthProvider>        );

What is the best way to actually pass in a useState to the modal?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>