tomas@wix.com
github.com/tqmukas
linkedin/miliauskas
// module print.js
module.exports = function print(msg) {
console.log(msg);
};
// module info.js
module.exports.name = 'Bob';
module.exports.age = 26;
// module app.js
var print = require('./print.js');
modules.exports = function printInfo() {
var info = require('./info.js');
print(info.name + ' ' + info.age);
}
setTimeout(printInfo, 1500);
// es6 modules
import React, {Component} from 'react';
export default App;
// destructuring
const {question, text} = data;
const [a, b] = [1, 2];
// arrow functions
const handleClick = (event) => {
};
// variables
let wrapper;
const guesses = [];
// classes
class App extends Component {
constructor() {
super();
}
}
// property shorthands
const foo = 'bar';
const data = {foo};
// array/object spread
const state = {...state, text: 'foo'};
const guesses = [...guesses, text];
// template strings
const text = `Lifes left: ${lives}`;
<!-- HTML Element -->
<button
class="primary"
>Submit</button>
// React Element
React.createElement(
'button',
{className: 'primary'},
'Submit'
);
<!-- Nested HTML -->
<div>
<input type="text">
<button
class="primary"
>Submit</button>
</div>
// Nested React
React.createElement('div',
React.createElement('input', {type: 'text'}),
React.createElement(
'button',
{className: 'primary'},
'Submit'
)
);
<!-- JSX Element -->
<button
className="primary"
>Submit</button>
<!-- Nested JSX -->
<div>
<input type="text">
<button
className="primary"
>Submit</button>
</div>
// Nested React
React.createElement('div',
React.createElement('input', {type: 'text'}),
React.createElement(
'button',
{className: 'primary'},
'Submit'
)
);
// React Element
React.createElement(
'button',
{className: 'primary'},
'Submit'
);
Build/Modify
Build/Modify
Send Events
Send Events
Template - JSX
Logic - JS,
Appearance - CSS, Assets - PNG, JSON
// Using ES5
var Alphabet = React.createClass({
render: function () {
return React.createElement(
'div', null, 'Alphabet'
);
}
});
ReactDOM.render(
<Alphabet/>,
document.getElementById('root')
);
// Using ES6
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class Alphabet extends Component {
render() {
return (<div>Alphabet</div>);
}
});
ReactDOM.render(
<Alphabet/>,
document.getElementById('root')
);
// simple values
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class DevDaysLink extends Component {
render() {
return (
<a
href="http://devdays.lt"
>DevDays 2017</a>
);
}
}
ReactDOM.render(
<DevDaysLink/>,
document.getElementById('root')
);
// variables
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class DevDaysLink extends Component {
render() {
const year = 2017;
const link = 'http://devdays.lt';
return (
<a href={link}>DevDays {year}<a>
);
}
}
ReactDOM.render(
<DevDaysLink/>,
document.getElementById('root')
);
import React, {Component} from 'react';
import PropTypes from 'prop-types';
class DevDaysButton extends Component {
render() {
const {link} = this.props; // Input through props
const {onButtonClick} = this.props // Output through events
return (<Button href={link}
onClick={(event) => onButtonClick(event)} // Synthetic event
>DevDays 2017</Button>);
}
}
DevDaysButton.propTypes = {
link: PropTypes.string.isRequired, // Required attribute
onButtonClick: PropTypes.func // Optional attribute
};
// Usage
ReactDOM.render(<DevDaysButton link="http://devdays.lt"
onButtonClick={event => console.log(event)}/>);
// Functional
import React from 'react';
function Hangman(props) {
return (
<img
src={props.src}
alt="hangman"
/>
);
}
export default Hangman;
// Class
import React, {Component} from 'react';
class Hangman extends Component {
render() {
return (
<img
src={this.props.src}
alt="hangman"
/>
);
}
}
export default Hangman;
// Mounting
constructor()
componentWillMount()
render()
componentDidMount()
// Unmounting
componentWillUnmount()
// Updating
componentWillReceiveProps()
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()
// How to use state properly
import React, {Component} from 'react';
class App extends component {
constructor(props) {
super(props);
this.state = {animal: 'Dog'}; // create state only in constructor
}
componentDidMount() {
this.setState({animal: 'Kitty'}); // modify state only via method
}
render() {
const {animal} = this.state; // state object to access properties
return (<div>Hello {animal}</div>);
}
}
// Regular styling
import React from 'react';
import './Button.css';
function PrimaryButton({label}) {
return (
<button
className="primary"
>{label}</button>
);
}
// Styling within CSS modules
import React from 'react';
import styles from './Button.css';
function PrimaryButton({label}) {
return (
<button
className={styles.primary}
>{label}</button>
);
}
/* Button.css */
.primary {
background-color: blue;
color: white;
}
Defines
Triggers
Sends
Updates
Contains
import React from 'react';
import TestUtils from 'react-dom/test-utils';
describe('Testing with React Test Utils', () => {
it('should render div as a DOM component', () => {
const div = TestUtils.renderIntoDocument(<div/>);
expect(TestUtils.isDOMComponent(div)).toBe(true);
);
});
// Shallow rendering
import { shallow } from 'enzyme';
const wrapper = shallow(<Component/>);
// Static rendering
import { render } from 'enzyme';
const wrapper = render(<Component/>);
// Full rendering
import { mount } from 'enzyme';
const wrapper = mount(<Component/>);
test('two plus two is four', () => {
expect(2 + 2).toBe(4);
});
// Or
it('two plus two is four', () => {
expect(2 + 2).toBe(4);
});
$ jest --env=jsdom
tomas@wix.com
github.com/tqmukas
linkedin/miliauskas