GithubHelp home page GithubHelp logo

context.locals or locals? about docs HOT 4 CLOSED

jamesli2021 avatar jamesli2021 commented on June 14, 2024
context.locals or locals?

from docs.

Comments (4)

sasoria avatar sasoria commented on June 14, 2024 1

Thank you for the issue, maybe we could try to keep the code examples as simple as possible and avoid destructuring?

At least in the first code example, the text above it becomes easier to understand:

Inside this file, export an onRequest() function that can be passed a context object and next() function. This must not be a default export.

export function onRequest (context, next) {
  ...
}

from docs.

Fryuni avatar Fryuni commented on June 14, 2024

I don't get your point, the two pages perfectly agree with each other. Which part is confusing you?

Is it because in the Astro example context is being destructured?

from docs.

jamesli2021 avatar jamesli2021 commented on June 14, 2024

I don't get your point too.

Could you please explain the meaning of 'context' within the term 'context.locals'?

What is the difference between context.locals and just write locals?

from docs.

Fryuni avatar Fryuni commented on June 14, 2024

There is no difference.

context is how the first argument of the middleware is called (the type is called APIContext). Same for API Endpoints.

// This
export const onRequest = (context, next) => {
  const locals = context.locals;
  locals.foo = 'bar';
};

// Is the same as this
export const onRequest = ({ locals }, next) => {
  locals.foo = 'bar';
};

It is just a more compact JS syntax called destructuring which can be used in many places including function parameters

from docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.