Piero Divasto
Full-stack developer
Full-stack Developer
Nisum
https://stateofjs.com/2017/front-end/results
const container =
document.getElementById('container');
const btn =
document.createElement('button');
btn.className = 'btn red';
btn.onclick = function(event) {
if(this.classList.contains('red')){
this.classList.remove('red');
this.classList.add('blue');
} else {
this.classList.remove('blue');
this.classList.add('red');
}
};
container.appendChild(btn);
class Button extends Component {
constructor() {
this.state = { color: 'red' };
}
handleClick = () => {
const color =
this.state.color === 'red' ? 'blue' : 'red';
this.setState({ color });
};
render() {
return (
<div>
<button
className=`btn ${this.state.color}`
onClick={this.handleClick}
/>
</div>
);
}
}
BlogPost
PostTitle
PostBody
PostComments
Comment
Comment
class HelloWorld extends React.Component {
render() {
return React.createElement(
"div",
null,
"Hello ",
this.props.name
);
}
}
ReactDOM.render(<HelloWorld name="World" />, mountNode);
<div>Hello World</div>
class HelloWorld extends React.Component {
render() {
return React.createElement(
"div",
null,
"Hello ",
this.props.name
);
}
}
ReactDOM.render(<HelloWorld name="World" />, mountNode);
class HelloWorld extends React.Component {
constructor(props) { super(props); }
render() {
return <div>Hello {this.props.name}</div>
}
}
ReactDOM.render(<HelloWorld name="World" />, mountNode);
Component
props
Properties of a component
Inmutables
Passed as parameter
class HelloWorld extends React.Component {
constructor(props) { super(props); }
render() {
return <div>Hello {this.props.name}</div>
}
}
ReactDOM.render(<HelloWorld name="World" />, mountNode);
Good practice
Defines the type of props
Generate warnings in browser
array | instanceOf(className) |
number | oneOf() |
string | oneOfType() |
bool | arrayOf() |
object | objectOf() |
node | shape() |
element | |
func |
class HelloWorld extends React.Component {
constructor(props) { super(props); }
render() {
return <div>Hello {this.props.name}</div>
}
}
HelloWorld.propTypes = {
name: PropTypes.string.isRequired
};
HelloWorld.defaultProps = {
name: ''
};
ReactDOM.render(<HelloWorld name="World" />, mountNode);
Component's internal state
NOT immutables
Changes in state are "merged"
Only owner component can modify it
class HelloWorld extends React.Component {
constructor(props) {
super(props);
this.state = {
param: '',
param2: ''
};
}
algunMetodo() {
this.setState({ param: 'new value' });
}
render() {
return <div>Hello {this.props.name}</div>
}
}
HelloWorld.propTypes = {
name: PropTypes.string.isRequired
};
HelloWorld.defaultProps = {
name: ''
};
ReactDOM.render(<HelloWorld name="World" />, mountNode);
Init
Update
Destruction
Init
Update
Destruction
getInitialState()
getDefaultProps()
componentWillMount()
render()
componentDidMount()
componentWillReceiveProps()
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()
componentWillUnmount()
Init
Update
Destruction
Misuses of methods
Conflict with future async rendering
componentWillMount
componentWillReceiveProps
componentWillUpdate
getSnapshotBeforeUpdate
componentWillMount
componentWillReceiveProps
componentWillUpdate
UNSAFE_*
https://twitter.com/dan_abramov/status/981712092611989509
const HelloWorld = props => {
return (
<div>Hello {props.name}</div>
)
}
HelloWorld.propTypes = {
name: PropTypes.string.isRequired
};
HelloWorld.defaultProps = {
name: ''
};
ReactDOM.render(<HelloWorld name="World" />, mountNode);
class HelloWorld extends React.Component {
constructor(props) { super(props); }
render() {
return <div>Hello {this.props.name}</div>
}
}
HelloWorld.propTypes = {
name: PropTypes.string.isRequired
};
HelloWorld.defaultProps = {
name: ''
};
ReactDOM.render(<HelloWorld name="World" />, mountNode);
http://github.com/morph3o
https://medium.com/@pierodivasto
https://www.linkedin.com/in/pierodivasto
By Piero Divasto
Presentation about React