By night, I am a creative coder. I love the web, and a lot of my work has to do with exploring how creativity and code intersect and interact with one another. My Twitter handle is MeloGood. You can see where I post a lot of experiments, especially observable notebooks. And my latest project is called Good Graphics, where I have been doing a lot of experimentation. And this talk kind of naturally arised out of some of my findings.
So, this quick overview of where we're headed. We're going to talk about the principle of UI, the functions, state, talk a little bit about SVGs, talk about them in the context of React components, and then talk about the graphics systems that you can build with them once you kind of have this understanding.
So, when we say UI, the function, state, it's a core React principle. You can render your entire site application as a function of the state that you give it. So, when you give it the first name, last name, it will render out the world. And as that input changes, your UI naturally re-renders and is always up to date with the state that it's giving. And it really just means that JXS allows us to declaratively write in mark-up, especially in the context of updates.
This is a very simple React component. It's called click-text. And really what it does is it assumes that you're going to give it a count and then as your back count updates, the text updates. So, when we have the prop of click equals one, it's just going to say click one time. As it has the prop of two, click two times, and so on and so forth. But really this illustrates that React allows us to really incorporate the state of the data that you were given and rendered on the screen without having to do too much extra.
Let's talk about SVGs. If you were like me before I started learning more about them, they're kind of just this design asset that my designer would give me and that I would just throw into a React component and just use but never really touch ever again. But it turns out they're a lot more than that. It's an XML-based language, they're describing 2D-based graphics which basically just means that there's a bunch of primitives that allow you to describe things like a circle or a rectangle on the screen. This need to be supported by JSX out of the box and the markup looks really similar to React components just with special props. So, in action this is an SVG. You see that we wrap the top level components SVG with a list for height, some details about how to draw lines with a stroke and a fill, and then you can see the circle component has a few props. The circle is x coordinate, the circle is y coordinate, and then r which is the answer radius. This is just the specification for how you would draw a circle using SVGs, and it kind of already follows and looks like a React component, so it lends itself very well to thinking in React. So we can start to build these SVGs out and kind of componentise them. We can add more logic to them and really start to work with graphics in the same context in a way that we already know how to work with our UIs.
Comments