Professional Documents
Culture Documents
ES6 Training
let y const, nuevas formas de declarar variables
var a = 5;
var b = 10;
if (a === 5) {
let a = 4; // El alcance es dentro del bloque if
var b = 1; // El alcance es global
console.log(a); // 4
console.log(b); // 1
}
console.log(a); // 5
console.log(b); // 1
const c = 20;
c = 30;
Template strings
let text = 'Hola como estas';
let name = 'Harold';
console.log(text + ' ' + name);
console.log(`${text} ${name}`);
Arrow functions
Funciones anónimas
(params) => valorDeRetorno
function foo () {
console.log('normal function');
}
bar = () => console.log('arrow function');
function foo(a, b) {
return a+b;
}
bar = (a, b) => (a + b);
Práctica en REPL
Url: https://jscomplete.com/repl/
1er ejemplo, uso de REPL (Read-Eval-Print-Loop)
const Button = function () {
return (
<button>Go</button>
);
};
Incrementando número
class Button extends React.Component {
constructor(props) {
super(props);
this.state = { counter: 0 };
}
render () {
return (
<button>{this.state.counter}</button>
)
}
}
ReactDOM.render(<Button />, mountNode);
Usando inicialización diferente y no en constructor (transform-class-properties babel
property)
handleClick = () => {
this.setState({
counter: this.state.counter + 1
});
};
Forma 1 solo Button
class Button extends React.Component {
state = { counter: 0 };
handleClick = () => {
this.setState((prevState) => ({
counter: prevState.counter + 1
}));
};
render () {
return (
<button onClick={this.handleClick}>
{this.state.counter}
</button>
)
}
}
state = { counter: 0 };
handleClick = () => {
this.setState((prevState) => ({
counter: prevState.counter + 1
}));
};
render () {
return (
<button onClick={this.handleClick}>
+1
</button>
);
}
}
render () {
return (
<button onClick={this.props.onClickFunction}>
+1
</button>
);
}
}
incrementCounter = () => {
this.setState((prevState) => ({
counter: prevState.counter + 1
}));
}
render () {
return (
<div>
<Button onClickFunction={this.incrementCounter} />
<Result counter={this.state.counter} />
</div>
);
}
}
handleClick = () => {
this.props.onClickFunction(this.props.incrementValue);
};
render () {
return (
<button onClick={this.handleClick}>
+{this.props.incrementValue}
</button>
)
}
}
Crear la app
create-react-app marvel-app
index.html
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
App.js
import './App.css';
const App = () => (
<Router>
<div>
<Route exact path="/" component={HeroCardList} />
<Route path="/:id" component={Detail} />
</div>
</Router>
);
api/api.js
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
const error = new Error(`HTTP Error ${response.statusText}`);
error.status = response.statusText;
error.response = response;
console.log(error);
throw error;
}
}
function parseJSON(response) {
return response.json();
}
`${URL}?apikey=${PUBLIC_KEY_MARVEL}&ts=${ts}&hash=${getHash(ts)}&limit=${LIMIT}`,
{
headers: {
Accept: "application/json"
}
}
)
.then(checkStatus)
.then(parseJSON)
.then(res => res.data.results);
};
`${URL}/${id}?apikey=${PUBLIC_KEY_MARVEL}&ts=${ts}&hash=${getHash(ts)}&limit=${LI
MIT}`,
{
headers: {
Accept: "application/json"
}
}
)
.then(checkStatus)
.then(parseJSON)
.then(res => res.data.results);
};
components/HeroCardList.js
componentDidMount () {
api.getMarvelComics().then((heroList) => {
this.setState({
heroList
});
});
}
render () {
return (
<div className="container">
<div className="row">
<div className="col s12 App-header">
Marvel Comics
</div>
</div>
<div className="row">
{
this.state.heroList.length > 0
?
this.state.heroList.map((hero) => (<HeroCard key={hero.id} hero={hero} />))
:
<h4>Cargando...</h4>
}
</div>
</div>
);
}
}
components/HeroCard.js
components/Detail.js
componentDidMount() {
api.getIndividualCharacter(this.props.match.params.id).then(hero => {
const firstHero = hero[0];
this.setState({
hero: firstHero
});
});
}
render() {
const hero = this.state.hero;
const ts = new Date().getTime();
return (
<div className="container">
<div className="row">
<div className="col s12 App-header">Marvel Description Character</div>
</div>
{hero && (
<div className="row center">
<div className="col s4">
<img
className="detail-image"
src={`${hero.thumbnail.path}.${hero.thumbnail.extension}`}
alt={hero.name}
/>
</div>
<div className="col s8">
<div className="card blue-grey darken-1">
<div className="card-content white-text">
<span className="card-title">{hero.name}</span>
<p>{hero.description}</p>
</div>
{hero.comics.items.length > 0 && (
<div>
<h6 className="detail-comic-list">Comics List</h6>
<div className="collection">
{hero.comics.items.map((comic, idx) => (
<a
key={idx}
href={`${comic.resourceURI}?apikey=${PUBLIC_KEY_MARVEL}&ts=${ts}&hash=${getHash
(ts)}&limit=30`}
target="_blank"
className="collection-item"
>
<span className="badge" />
{comic.name}
</a>
))}
</div>
</div>
)}
</div>
</div>
</div>
)}
</div>
);
}
}
App.css
.App-header {
display: flex;
font-size: 20px;
background-color: #222;
height: 50px;
padding: 20px;
color: white;
justify-content: center;
align-items: center;
}
.App-image {
height: 300px;
width: 180px;
}
.App-description {
height: 80px;
text-overflow: ellipsis;
overflow: hidden;
}
.bg-card {
display: flex;
justify-content: center;
align-items: center;
font-size: 16px !important;
background: #000;
padding: 16px !important;
height: 20px;
width: 100%;
}
.detail-image {
height: 200px;
width: 200px;
border-radius: 50%;
border: 1px solid #000;
}
.detail-comic-list {
color: white;
font-size: 20px;
}