Keyword Args & Immutability
Explore how to use keyword arguments with __pragma__ directives in Transcrypt and understand the handling of immutable objects in a React front-end environment. This lesson helps you manage state updates and use techniques like copying immutable props for effective front-end development.
We'll cover the following...
Keyword arguments
By default, unpacking keyword arguments with **kwargs in a function does not work out of the box in Transcrypt. This is because it would lead to bloated code when it gets transpiled to JavaScript. Fortunately, Transcrypt provides us with a compiler directive that will selectively enable that feature for just when it’s needed. Here is a simple example:
So, we basically just have to wrap our function that uses **kwargs in the __pragma__ compiler directives to turn it on, then turn it off again. Transcrypt will then make sure that the function is transpiled into JavaScript with that capability enabled.
If we do try and use **kwargs without the __pragma__ compiler directives, we might see an error similar to this in the web browser console at runtime:
ReferenceError: kwargs is not defined
...