diff --git a/content/blog/angular-components-control-value-accessor/index.md b/content/blog/angular-components-control-value-accessor/index.md index 5a507960..aedecfa9 100644 --- a/content/blog/angular-components-control-value-accessor/index.md +++ b/content/blog/angular-components-control-value-accessor/index.md @@ -70,7 +70,7 @@ export class ExampleInputComponent { With only a bit of CSS, we have a visually appealing, A11Y friendly, and quirky input component. Look, it even wiggles the unicorns! - + Now, this component is far from feature complete. There's no way to `disable` the input, there's no way to extract data out from the typed input, there's not a lot of functionality you'd typically expect to see from an input component. Let's change that. @@ -303,7 +303,7 @@ Finally, you can pass these options to `ngModel` and `formControl` (or even `for If done properly, you should see something like this: - + # Form Control Classes @@ -407,7 +407,7 @@ export class AppComponent { } ``` - + Not only do you have [a wide range of Angular-built validators at your disposal](https://angular.io/api/forms/Validators), but you're even able to [make your own validator](https://angular.io/api/forms/Validator)! diff --git a/content/blog/angular-templates-start-to-source/index.md b/content/blog/angular-templates-start-to-source/index.md index f2a863db..dd4cfd1c 100644 --- a/content/blog/angular-templates-start-to-source/index.md +++ b/content/blog/angular-templates-start-to-source/index.md @@ -54,7 +54,7 @@ While Angular templates come in many shapes and sizes, a simple but common use f
True
``` - + In this example, we are creating a template and assigning it to a [template reference variable](https://blog.angulartraining.com/tutorial-the-magic-of-template-reference-variables-3183f0a0d9d1). _This template reference variable makes `falseTemp` a valid variable to use as a value for other inputs in the same template._ It then handles that variable similarly to how a variable from the component logic is handled when referenced from the template. @@ -80,7 +80,7 @@ But there's a ~~simpler~~ ~~much more complex~~ another way show the same templa{{upperInUpper}}: {{msg}}
``` - + In this example, we're expecting `'upper'` to be turned into `'UPPER'` by the `uppercase` pipe, then to be passed as the input to `makePiglatinCasing` and for the `$implicit` value of that context to be assigned to a local variable `msg`. If you load this, you'll noticed that the uppercased pig lattin displays as expected but the `upperInUpper` variable (which we expected to be `'UPPER'`) is undefined. @@ -1608,7 +1608,7 @@ this.parentViewRef.createEmbeddedView(this.templ, { }); ``` - + Now that we're exporting the output with the `as`, it should show on-screen as expected. So why is this? **Well, `as` exports the outputted value that it's bound to.** In this case, we're binding the value to `casing` (because that's what `'upper'` is being passed as an input to). @@ -1818,7 +1818,7 @@ export class AppComponent { } ``` - + - We're starting with enabling `uniFor` as the structural directive name - Then we're defining an input to accept `of` as a key in the syntax (to match the `ngFor` structural directive syntax). diff --git a/content/blog/how-to-upgrade-to-react-18/index.md b/content/blog/how-to-upgrade-to-react-18/index.md index 2e181762..3ce43910 100644 --- a/content/blog/how-to-upgrade-to-react-18/index.md +++ b/content/blog/how-to-upgrade-to-react-18/index.md @@ -78,7 +78,7 @@ ReactDOM.createRoot(rootElement).render( When finished, you should be able to verify the version of React you’re using with `{React.version}` - + ## Conclusion diff --git a/content/blog/master-react-unidirectional-data-flow/index.md b/content/blog/master-react-unidirectional-data-flow/index.md index 35ad3e2d..38f6a687 100644 --- a/content/blog/master-react-unidirectional-data-flow/index.md +++ b/content/blog/master-react-unidirectional-data-flow/index.md @@ -29,7 +29,7 @@ A great example of a set of components we'll use to demonstrate unidirectionalit Let's take a look at a code sample that follows this unidirectionality first: - + As you can see we're passing the `onChange` and value props to `SimpleForm`. This keeps our state consolidated inside of the `App` component rather than split between `App` and `SimpleForm`. Once you "submit" the form, `SimpleForm` calls `onDone` which changes the state stored inside of `App`. This in turn causes a re-render of `SimpleForm`. @@ -94,7 +94,7 @@ export default function App() { } ``` - + This code works, but has some inherent complexity issues. When you start expanding this component, this idea of separating your state and having to inspect the child reference from the parent makes development more difficult. Let's take a look visually how following the application logic is now more difficult with this pattern. @@ -133,7 +133,7 @@ Understanding unidirectionality is integral to scaffolding scalable React applic Now that we have a deeper understanding of unidirectionality, here's a challenge for you: Refactor the following components to better reflect unidirectionality in this coding pad. - + The functionality of the app should be consistent with the previous version. Stuck? diff --git a/content/blog/python-list-comprehension-guide/index.md b/content/blog/python-list-comprehension-guide/index.md index 04161014..458d5015 100644 --- a/content/blog/python-list-comprehension-guide/index.md +++ b/content/blog/python-list-comprehension-guide/index.md @@ -239,7 +239,7 @@ We’ve covered a lot about list comprehension in Python today! We’re able to For example, given this sandbox code pad of a long and messy list comprehension, how can you refactor to remove all usage of list comprehensions? Avoid using `map`, `filter` or other list helpers, either. Simply use nested `for` loops and `if` conditionals to match the behavior as it was before. - + This is an open-ended question meant to challenge your skills you’ve learned throughout the article! diff --git a/content/blog/react-refs-complete-story/index.md b/content/blog/react-refs-complete-story/index.md index 3ebf19e6..b377a849 100644 --- a/content/blog/react-refs-complete-story/index.md +++ b/content/blog/react-refs-complete-story/index.md @@ -113,7 +113,7 @@ Thanks to the lack of rendering on data storage, it's particularly useful for st }, [dataRef]); ``` - + # Visual Timer with Refs {#visual-timers} @@ -145,7 +145,7 @@ Let's take the example from before, but inside of the `setInterval`, we update a Now, we'd expect to see the timer update from `1` to `2` (and beyond) as the timer continues to render. However, if we look at the app while it runs, we'll see some behavior we might not expect: - + This is because [the closure](https://whatthefuck.is/closure) that's passed to the `setInterval` has grown stale. This is a common problem when using React Hooks. While there's a simple solution hidden in `useState`'s API, let's solve this problem using mutations and `useRef`. @@ -171,7 +171,7 @@ Because `useRef` relies on passing by reference and mutating that reference, if }, [dataRef]); ``` - + > * I would not solve it this way in production. `useState` accepts a callback which you can use as an alternative (much more recommended) route: > @@ -214,7 +214,7 @@ At the start of this article, I mentioned that `ref`s are not just a mutable dat In this example, if we took a look at the `console.log` in the `useEffect`, we'd find [an `HTMLDivElement` instance](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDivElement) in the `current` property. Open the following StackBlitz and look at the console value to confirm: - + Because `elRef.current` is now a `HTMLDivElement`, it means we now have access to [the entire `Element.prototype` JavaScript API](https://developer.mozilla.org/en-US/docs/Web/API/Element#Properties). As such, this `elRef` can be used to style the underlying HTML node: @@ -230,7 +230,7 @@ Because `elRef.current` is now a `HTMLDivElement`, it means we now have access t ) ``` - + ## Alternative Syntax {#ref-function} @@ -272,7 +272,7 @@ const App = () => { ); ``` - + You might be wondering why I didn't call that property `ref` instead of `divRef`. This is because of a limitation with React. If we try to switch the property's name to `ref`, we find ourselves with some unintended consequences. @@ -297,7 +297,7 @@ const App = () => { ); ``` - + You'll notice that the `Container` `div` is not styled to have a `lightblue` background. This is because `elRef.current` is never set to contain the `HTMLElement` ref. As such, for simple ref forwarding, you cannot use the `ref` property name. @@ -325,7 +325,7 @@ const App = () => { Now that we are using `forwardRef`, we can use the `ref` property name on the parent component to get access to the `elRef` once again. - + # Class Component References {#class-ref} @@ -379,7 +379,7 @@ const App = () => { > } > ``` - + If you look at the `console.log` statement, you'll notice that it prints something like this: @@ -464,7 +464,7 @@ function App() { } ``` - + # Unidirectional Flow {#unidirectional-flow} @@ -637,7 +637,7 @@ export default function App() { } ``` - + As you can witness from the embedded demo it will focus you on the `Container` `div` when the application renders. This example does not use the `useImperativeHandle` hook but instead relies on the timing of `useEffect` to have the `ref`'s `current` already defined. @@ -680,7 +680,7 @@ export default function App() { } ``` - + > If you look in the console, you'll find the `console.log` has run when `focus()` ran! @@ -704,7 +704,7 @@ That said, you're not limited to simply the names of native APIs. What do you th }, [elRef]) ``` - + > When your focus is set to the `Container` element, try typing in the ["Konami code"](https://en.wikipedia.org/wiki/Konami_Code) using your arrow keys. What does it do when that's done? @@ -777,7 +777,7 @@ export default function App() { } ``` - + This code behaves as we might initially expect, not because we've done things properly, but instead, thanks to the nature of React's `useEffect` hook's timing. @@ -811,7 +811,7 @@ export default function App() { } ``` - + Oh no! The background is no longer `'lightblue'`! Because we delay the rendering of the `div`, `elRef` is _not_ assigned for the initial render. Then, once it _is_ rendered, it mutates the `.current` property of `elRef` to assign the ref. Because mutations do not trigger a re-render (and `useEffect` only runs during renders), `useEffect` does not have a chance to "compare" the differences in value and, therefore, run the side-effect. @@ -838,7 +838,7 @@ Confused? That's okay! So was I at first. I made a playground of sorts to help u }, [minus]); ``` - + > Open your console and take notes of what `console.log` runs when you change the respective values! @@ -907,7 +907,7 @@ But, you're probably thinking that if it accepts a function, we could pass a cal ); ``` - + > But hey! Wait a minute! Even though the `shouldRender` timing mismatch is still there, the background is being applied all the same! Why is the `useEffect` timing mismatch not causing the bug we were experiencing before? @@ -934,7 +934,7 @@ That's true. However, you _can_ combine the two behaviors to make a callback tha }, [elRef, shouldRender]); ``` - + # `useState` Refs {#usestate-refs} @@ -958,7 +958,7 @@ You can do this relatively trivially using callback refs to assign to a `useStat }, [elRef]) ``` - + Now that the `ref` update causes a re-render, you can now _**safely**_ use the `ref` in `useEffect`'s dependency array. @@ -977,7 +977,7 @@ Now that the `ref` update causes a re-render, you can now _**safely**_ use the ` }, [elNode]) ``` - + However, this comes at an offset cost of performance. Because you're causing a re-render, it will inherently be slower than if you were not triggering a re-render. There are valid uses for this, however. You just have to be mindful of your decisions and your code's usage of them. diff --git a/content/blog/rust-enums-matching-options-api/index.md b/content/blog/rust-enums-matching-options-api/index.md index 4330eb3a..8c2c871f 100644 --- a/content/blog/rust-enums-matching-options-api/index.md +++ b/content/blog/rust-enums-matching-options-api/index.md @@ -499,7 +499,7 @@ Let’s say that we have the “patch” version of a software tracked. We want How can you modify the code below to support the match version being listed out properly? - + You’ll know the code is working when you’re able to output the following: diff --git a/content/blog/understanding-the-dom/index.md b/content/blog/understanding-the-dom/index.md index 44a7c168..43df252b 100644 --- a/content/blog/understanding-the-dom/index.md +++ b/content/blog/understanding-the-dom/index.md @@ -469,7 +469,7 @@ However, as you can see, we're running `stopPropagation` on the event in the blu You can see a running example of this here: - + ### Capturing {#event-capturing} @@ -510,7 +510,7 @@ This means that when the user clicks on the red square, you'll see the following You won't see anything from the green square's `eventListener`, however. - + You'll also notice that if you click on the green square, you'll never see the `"A click handled on green using capture"` message. This is due to the `stopPropagation`, as mentioned before. The click is being registered on the red square first and then stopped on the blue square. diff --git a/content/blog/web-components-101-lit-framework/index.md b/content/blog/web-components-101-lit-framework/index.md index 8e3ef73f..bacec9f3 100644 --- a/content/blog/web-components-101-lit-framework/index.md +++ b/content/blog/web-components-101-lit-framework/index.md @@ -65,7 +65,7 @@ window.customElements.define('hello-component', HelloElement);