title: Put the knife down and take a green herb, dude. |
descrip: One feller's views on the state of everyday computer science & its application (and now, OTHER STUFF) who isn't rich enough to shell out for www.myfreakinfirst-andlast-name.com Using 89% of the same design the blog had in 2001. |
FOR ENTERTAINMENT PURPOSES ONLY!!!
Back-up your data and, when you bike, always wear white. As an Amazon Associate, I earn from qualifying purchases. Affiliate links in green. |
|
x
MarkUpDown is the best Markdown editor for professionals on Windows 10. It includes two-pane live preview, in-app uploads to imgur for image hosting, and MultiMarkdown table support. Features you won't find anywhere else include...
You've wasted more than $15 of your time looking for a great Markdown editor. Stop looking. MarkUpDown is the app you're looking for. Learn more or head over to the 'Store now! |
|
Sunday, December 11, 2022 | |
In the last post, we discussed how to set up a Preact project by importing Preact and HTM libraries in "1999-style JavaScript" to help us understand how to modernize a legacy client codebase and/or introduce modern templating conventions to developers who have never used them before. This pattern allows us to get a componentized paradigm up and running without any serious overhead. Nobody will ask, "What is npm?" or "Why isn't webpack working?" Using Preact and HTM allows for a perfect simplest case, introducing nothing beyond what's immediately needed, but being able to deploy immediately, if that's the goal. But we only got so far, with one simplest-case component in a static system. Let's make things a little more complicated,
WARNING: I'm posting this in reverse chronological order so they appear correctly in the blog, which lists the most recent first. Components nesting in componentsPreviously, we talked about how the HTM project uses JavaScript tagged templates to do the work React programmers usually accomplish with JSX, ending with a sort of lament that we've got to use so many The Let's look at an example that adds a new ParentComponent.js
MyComponent.js No changes to this component from last time.
index.html
Note that if this was JSX, we wouldn't need to surround the component names with JSX
That is nicer. But not valid JavaScript! The parser is going to puke as soon as it hits that first HTM
Not too bad. But note that if we wanted to use closing tags, we don't wrap those. Fun, huh? We won't need them until we add children to the components, but this is functionally the same as the HTM section, above:
I'd almost... almost... prefer to use Adding children to componentsLet's add a way to pass children to ParentComponent.js
MyComponent.js I'm going to add a variable to hold the children if they exist (like in Notice that I'm using a tagged template processed by
Result:
Best practice: Only have html in your leavesThere a best practice in to point out here: You should try to keep html only in the "leaf" components. That is, think of each component as a node, and let's say that each node can be either 1. a leaf or 2. a branch, and that you can tell which type of node of you have by asking, "Does this component's rendering include another Preact component?" If a component does contain another component, it's a branch with more component nodes below. If it doesn't, and the rendered HTM contains only html, it's a leaf. Let me propse that, in the best case, only your leaf components should include html. This helps keep your UI simple, readable, and, until the very last minute, properly abstract. In our example, we have one
Now Past html to be sent and rendered in the leaf, though, no html in a branch! Best practice: CSSCSS is an interesting situation. The first rule for CSS in Preact is no inline CSS in preact! We generally want to use stylesheets to assign styles. We can either hardcode class names into a leaf component or we can pull classes out of our props, if that's useful. Let's add some css to our
If you reload the page after just that, you'll notice that the paragraph with "This is some normally-sized text." in it has already adopted a grey background, just like you'd expect. This is why we can, say, introduce bootstrap straightforwardly, just as we normally would with static html. For some more dynamism, let's make so that we can pass in the MyComponent.js
ParentComponent.js
And if you wanted to pass in two classes, nothing fancy; it's just a string.
Now look, I know I said don't use inline styles, but just so you can recognize when someone does, you do it similarly to Here's an example for MyComponent.js
Again, the interesting thing to note here is that you use an object. I've been able to use keys that both match the style attribute I want to set ( Labels: css, howto, javascript, preact, tutorial posted by ruffin at 12/11/2022 08:07:00 AM |
|
| |
All posts can be accessed here: Just the last year o' posts: |
||||||||||||||||||||||
|