The V in MVC
React is a library, not a framework which basically means that you'll need to use it in conjunction with other libraries or, at least, vanilla JS to make it work for complex applications.
The virtual DOM is used for efficient re-rendering of the DOM.
{
type: 'button',
props: {
className: 'button button-blue',
children: {
type: 'b',
props: {
children: 'OK!'
}
}
}
}<button class='button button-blue'>
<b>
OK!
</b>
</button>is a plain object representation of
A ReactNode can be either:
These are used as properties of other ReactElements to represent children. Effectively they create a tree of ReactElements.
An element describing a component is also an element, just like an element describing the DOM node. They can be nested and mixed with each other.
const DeleteAccount = () => ({
type: 'div',
props: {
children: [{
type: 'p',
props: {
children: 'Are you sure?'
}
}, {
type: DangerButton,
props: {
children: 'Yep'
}
}, {
type: Button,
props: {
color: 'blue',
children: 'Cancel'
}
}]
});
However, whether functions or classes, fundamentally they are all components to React. They take the props as their input, and return the elements as their output.
const Form = ({ isSubmitted, buttonText }) => {
if (isSubmitted) {
// Form submitted! Return a message element.
return {
type: Message,
props: {
text: 'Success!'
}
};
}
// Form is still visible! Return a button element.
return {
type: Button,
props: {
children: buttonText,
color: 'blue'
}
};
};We could define a "Form" component as follows
ReactDOM.render({
type: Form,
props: {
isSubmitted: false,
buttonText: 'OK!'
}
}, document.getElementById('root'));// React: You told me this...
{
type: Form,
props: {
isSubmitted: false,
buttonText: 'OK!'
}
}
// React: ...And Form told me this...
{
type: Button,
props: {
children: 'OK!',
color: 'blue'
}
}
// React: ...and Button told me this!
// I guess I'm done.
{
type: 'button',
props: {
className: 'button button-blue',
children: {
type: 'b',
props: {
children: 'OK!'
}
}
}
}