Ofir Dagan
@ofirdagan2
ofird@wix.com
Ofir Dagan
@ofirdagan2
ofird@wix.com
React.createElement('div', null, `Sveika pasaule`);
<div>Sveika pasaule</div>
React.createElement(
'div',
{className: 'title'},
`Sveika pasaule`
);
<div className="title">
Sveika pasaule
</div>
React.createElement(
'div',
{className: 'wrapper'},
React.createElement(Person, {age: 56}, null),
React.createElement(Person, {age: 65}, null)
);
<div class="wrapper">
<Person age="56"/>
<Person age="65"/>
</div>
const helloWorld = React.createElement('div', null, `Sveika pasaule`);
ReactDOM.render(helloWorld, document.getElementById('root'));
Will (not) surprisingly result with
+---+ element = div
|
|
+--> children = [Sveika pasaule]
const Hello = function ({name}) {
return React.createElement('div', null, `Hello ${name}`);
};
const hello = React.createElement(Hello, {name: 'Ofir'}, null);
ReactDOM.render(helloWorld, document.getElementById('root'));
-> Hello Ofir
+---+ element = hello
|
|
+---+ props = {name: 'Ofir'}
|
|
+--+> children = [div]
|
|
+--> children = [Hello Ofir]
class Hello extends React.Component {
constructor() {
super();
this.state = {};
this.fetchName();
}
fetchName() {
setTimeout(() => this.setState({name: 'Latvia'}), 500);
}
render() {
return React.createElement('div', null, `Hello ${this.state.name}`);
}
}
const helloWorld = React.createElement(Hello, null, null);
ReactDOM.render(helloWorld, document.getElementById('root'));
Output
------
-> Hello undeifned
--- 500ms passes
-> Hello Latvia
class NameComponent extends React.Component {
render() {
return (
<h1>Hello {this.props.name}</h1>
);
}
}
React.render(
<NameComponent name="Boki"/>,
document.getElementById('root')
);
-> Hello Boki
class NameComponent extends React.Component {
constructor(props) {
super(props);
this.props.name = 'Foo'; // ----------- NEW LINE ---------------
}
render() {
return (
<h1>Hello {this.props.name}</h1>
);
}
}
React.render(
<NameComponent name="Boki"/>,
document.getElementById('root')
);
-> Hello Foo
class NameComponent extends React.Component {
constructor(props) {
super(props);
this.props.name = 'Foo';
setTimeout(() => {
this.props.name = 'Moki' // ----------- NEW LINE ---------------
});
}
render() {
return (
<h1>Hello {this.props.name}</h1>
);
}
}
React.render(
<NameComponent name="Boki"/>,
document.getElementById('root')
);
-> Hello Foo
class NameComponent extends React.Component {
constructor(props) {
super(props);
this.props.name = 'Foo';
setTimeout(() => {
this.props.name = 'Moki'
this.setState({bla: '1'}); // ----------- NEW LINE ---------------
});
}
render() {
return (
<h1>Hello {this.props.name}</h1>
);
}
}
React.render(
<NameComponent name="Boki"/>,
document.getElementById('root')
);
-> Hello Moki
We rendered:
We implemented:
Ofir Dagan
@ofirdagan2
ofird@wix.com