Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js what is the "ctx" object and where does it come from?

I have seen the dubious "ctx" object in React sometimes as a parameter, like in this _app.js: https://spectrum.chat/next-js/general/fetch-data-once-in-app-js~d76c5ea2-1a6f-4719-b476-a05765c726d1

and I am wondering what it exactly is and where does it come from, as I have seen no good documentation about what it is... is it the same as props passed to some react component?

like image 724
Marc_L Avatar asked Sep 01 '25 15:09

Marc_L


1 Answers

ctx is a NextJS artifact. It is not part of standard React.

ctx is a context object containing those properties (Source):

  • pathname - path section of URL
  • query - query string section of URL parsed as an object
  • asPath - String of the actual path (including the query) shows in the browser
  • req - HTTP request object (server only)
  • res - HTTP response object (server only)
  • err - Error object if any error is encountered during the rendering

This may also help you.

like image 94
HeySora Avatar answered Sep 14 '25 14:09

HeySora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!