Tree Diffing
: Fast comparison and patching of data by comparing the current virtual DOM and new virtual DOM - updating only the pieces that change.It's just a tree with some fancy diffing
From JavaScript To DOM
React.createElement
function has the following form:Type
: Type of element to create, i.e. a string for an HTML element or a reference to a function or class that is a React component.Props
: Object that contains data to render the element.Children
: Children of the elemet, as many as you want.Creating elements
<ul>
<li class="selected">
<a href="/pets">Pets</a>
</li>
<li>
<a href="/owners">Owners</a>
</li>
</ul>
There are five tags to create:
ul
li
a
There are certain attributes we want to appear in the DOM for these tags as well:
li
has a class
(or className
in React)a
ele’s have href
attributesul
is the parent of both li
li
has an a
element as a childa
has a text content
childReact.createElement(
"ul",
null,
React.createElement(
"li",
{ className: "selected" },
React.createElement("a", { href: "/pets" }, "Pets")
),
React.createElement(
"li",
null,
React.createElement("a", { href: "/owners" }, "Owners")
)
);
Converting to virtual DOM
React.createElement
, you use React.render
to take the value returned from cE and a DOM node to insert into the conversion of the real DOM.// Put the element tree in a variable
const navList = React.createElement(
"ul",
null,
React.createElement(
"li",
{ className: "selected" },
React.createElement("a", { href: "/pets" }, "Pets")
),
React.createElement(
"li",
null,
React.createElement("a", { href: "/owners" }, "Owners")
)
);
// Get a DOM node for React to render to
const mainElement = document.querySelector("main");
// Give React the element tree and the target
ReactDOM.render(navList, mainElement);
Updates
Thinking in Components
React.createElement Demo
import 'package-link'
const App = () => React.createElement("h1", null, "Hello, Programmers!");
const target = document.querySelector("main");
const app = React.createElement(App, null);
// Give React the element tree and the target
ReactDOM.render(app, target);
HTML Original
<section class="clue">
<h1 class="clue__title">Clue$ 268530</h1>
<div class="clue__question">
2009: I dreamed a Dream
</div>
<div class="clue__category">
<<unparsed>>
</div>
<div class="clue__amount">
$800
</div>
</section>
React Version
const Clue = () =>
React.createElement(
"section",
{ className: "clue" },
React.createElement("h1", { className: "clue__title" }, "Title"),
React.createElement("div", { className: "clue__question" }, "?"),
React.createElement("div", { className: "clue__category" }, "Category"),
React.createElement("div", { className: "clue__amount" }, "$800")
);
class
is a reserved keyword in JS, in React we can use className
to assign a class to an element.props
: Properties;defaultProps
.You can change in the devTools Network tab the internet speed to check for values that may be undefined to hangle with defaultProps.
map
.clue => { key:clue.id, ...clue }
const App = (props) =>
React.createElement(
"h1",
null,
props.clues.map((clue) =>
React.createElement(Clue, { key: clue.id, ...clue })
)
);
export default App;
When you import modules from websites they must have CORs activated.
global variables
.When we want to move our code into production we need to change the imports into the production minified versions.
import "https://unpkg.com/react@16/umd/react.production.min.js";
import "https://unpkg.com/react-dom@16.13.1/umd/react-dom.production.min.js";
import "https://unpkg.com/react@16/umd/react.development.js";
import "https://unpkg.com/react-dom@16/umd/react-dom.development.js";
const Links = () =>
React.createElement(
"ul",
{ id: "nav-links" },
React.createElement(
"li",
{ className: "is-selected" },
React.createElement("a", { href: "http://appacademy.io" }, "App Academy")
),
React.createElement(
"li",
null,
React.createElement("a", { href: "https://aaonline.io" }, "a/A Open")
)
);
// Set up React Element: Type, Imported Data, Child (Child is Text in this Scenario)
// HelloWorld is a function based component
const HelloWorld = () => React.createElement("h1", null, "Hello, Programmers");
const AllTogether = () =>
React.createElement(
"div",
null,
React.createElement(HelloWorld, null),
React.createElement(Links, null)
);
// Target the Element to append new Ele
const target = document.querySelector("main");
// Assign your 'App' to your created Elements
// We are creating an element from the HelloWorld function.
const app = React.createElement(AllTogether, null);
// Render from the Virtual Dom to the Actual Dom
ReactDOM.render(app, target);
JSX
: Javascript Extension, a new language created by React developers to have an easier way of interacting with the React API.How to use JSX
babel
to convert version of modern JS into an older version of JS. React Create Elementconst ExampleComponent = (props) =>
React.createElement(
React.Fragment,
null,
React.createElement("h1", null, "Hello!"),
React.createElement("img", { src: "https://via.placeholder.com/150" }),
React.createElement("a", { href: props.searchUrl }, props.searchText)
);
JSX Version
const ExampleComponent = (props) => (
<React.Fragment>
<h1>Hello!</h1>
<img src="https://via.placeholder.com/150" />
<a href={props.searchUrl}>{props.searchText}</a>
</React.Fragment>
);
Keep in mind that self closing tags in React must have a forward slash
to close it.
Properties and Data
<img src="https://via.placeholder.com/150" />;
// becomes..
React.createElement("img", { src: "https://via.placeholder.com/150" });
// if we want to pass in data vs just a string literal
<a href={props.searchUrl}>{props.searchText}</a>;
// so it becomes..
React.createElement("a", { href: props.searchUrl }, props.searchText);
// if you want the text search uppercase..
<a href={props.searchUrl}>{props.searchText.toUpperCase()}</a>;
Property Names
:
checked
: Attribute of input components such as checkbox or radio, use it to set whether the component is checked or not.className
: Used to specify a CSS class.dangerouslySetInnerHTML
: React’s equivalent of innerHTML because it is risky to cross-site scripting attacks.htmlFor
: Because for
is protected keyword, React elements use this instead.onChange
: Event fired whenever a form field is changed.style
: Accepts a JS object with camelCase properties rather than a CSS string.value
: Supported by Input, Select, and TextArea components; use it to set the value of the component.Note: React uses camel-case!!!
The JSX semicolon gotcha
create Element equivalent
is equivalent to
function App() {
return (
React.createElement(
'div',
null,
React.createElement('h1', null, 'Hello!'),
React.createElement('div', null, 'Welcome to JSX.'),
)
);
}
npx create-react-app my-app
React.createElement(
"a",
{
className: "active",
href: "https://appacademy.io",
},
"App Academy"
);
// JSX Version
<a className="active" href="https://appacademy.io">
App Academy
</a>;
React.createElement(
OwnerDetails,
{
owner: props.data.owner,
number: props.index + 1,
},
props.name
);
// JSX Version
<OwnerDetails owner={props.data.owner} number={props.index + 1}>
{props.name}
</OwnerDetails>;
More Complex JSX Example
const BookPanel = (props) => {
<section className="book" id={`book-${props.id}`}>
<h1 className="book__title">{props.title}</h1>
<img src={props.coverUrl} />
<div className="book__desc">{props.description}</div>
</section>;
};