So for example, I’m building a small application that generates colour schemes, and I’m using React to display everything.
The main part of the application doesn’t have anything to do with React. For example, the key data structure is an object that looks like this:
const exampleColour = {
id: djti7ff6h0, // pseudo-random ID
hsl: { h: 270, s: 50, l: 40 },
rgb: { r: 40, g: 20, b: 60 },
hex: "#663399",
css: {
hsl: "hsl(270, 50%, 40%)",
hsla: "hsla(270, 50%, 40%, 1)",
rgb: "rgb(40, 20, 60)",
rgba: "rgba(40, 20, 60, 1)",
},
}
I have functions that can build this object from hex, RGB and HSL input. I have functions can modify it and rebuild it with new values, and so on. This is my application logic.
Just to emphasise, this is all just basic JS, neither the object nor any of the functions that create it or operate on it have anything to do with React.
The UI for one colour will look something like this:
import React from "react";
const Swatch = ({colour}) => (
<svg className=`swatch swatch-${colour.id}` viewBox="0 0 100 100">
<rect style={ `fill: ${colour.hex}` } width="100" height="100">
</svg>
);
export Swatch;
And used like
<Swatch colour={ exampleColour } />
The React components just have to be given an object that looks like the example one: they aren’t tied to any of the functionality of the colour generation/modification. And with UI elements that control changes to the colour (buttons, inputs etc), all they do is call the colour generation/modification functions: again, they don’t need to know what those functions do. React components (UI code) call the functions. These change the data (application logic). Then when the data changes, React handles updating the UI and the cycle can begin again.