Presented by Guyllaume Cardinal
Partner, brand creator and frontend expert
Majisti 3 years, Web dev 7 years
React for more than a year
Elaborated all of Majisti's frontend stack
Writer on BoobOrGuru.com
Get in touch: gy@majisti.com or on LinkedIn!
The developers you wish you had hired first
Blog on our processes, thoughts and tips and tricks.
Sometimes we rant too.
17:30 to 19:00 - Networking
19:00 to 20:00 - Presentation
20:00 to eternity - Pub Victoria!
I'm using a bit of ES6 in my examples, lets quickly go over a few simple concepts
function add(a, b) {
return a + b;
}
// Can become
var add = (a, b) => a + b;
function addTen(a) {
return a + 10;
}
// Can become
var add = a => a + 10;
A simpler way to write functions. Be careful, it CAN get messy if you abuse it.
Sugar on top of regular JavaScript objects. Allows some features of object-oriented languages
class Person {
constructor(name) {
this.name = name;
}
talk() {
console.log(this.name);
}
}
var guyllaume = new Person('Guyllaume');
guyllaume.talk();
A JavaScript library for building interfaces
import * as React from 'react'
import * as ReactDOM from 'react-dom'
ReactDOM.render(
<FilterableProductTable>
<SearchBar />
<ProductTable>
<ProductCategory>Sporting Goods</ProductCategory>
<ProductRow name='Football' price='49.99' />
<ProductRow name='Baseball' price='9.99' />
<ProductRow name='Basketball' price='29.99' outOfStock />
</ProductTable>
</FilterableProductTable>
, document.getElementByID('react-root'))
import * as React from 'react'
import * as ReactDOM from 'react-dom'
class HelloMessage extends React.Component {
render() {
return <div>Hello {this.props.name}</div>;
}
}
ReactDOM.render(<HelloMessage name="John" />, mountNode);
import * as React from 'react'
import AppBar from 'material-ui/AppBar'
ReactDOM.render(
<AppBar
title="Title"
iconClassNameRight="muidocs-icon-navigation-expand-more"
/>,
mountNode
);
// JS
render() {
let label = 'Submit'
let iconClass = 'user-icon'
return (
<h1>Foobar</h1>
<IconButton>
<Icon class={iconClass} />
<Label>{label}</Label>
</IconButton>
)
}
<!-- HTML -->
<h1>Foobar</h1>
<button>
<i class="user-icon"></i>
Submit
</button>
React has a very small, but efficient, API making it easy for anyone to jump in and quickly make sense of everything.
There are very few concepts to learn. In fact, let's go through all of them.
The internal state (duh) of a component
class Button extends React.Component {
constructor(props) {
super(props)
this.state = { label: 'Foo' }
}
render() {
return (
<button onClick={() => this.setState({label: 'bar'})}>
{this.state.label}
</button>
)
}
}
Passed down to all children of a component (even multiple levels down). Easier way to pass down properties to children down the render tree, but should almost never be used.
class Button extends React.Component {
render() {
return (
<button style={{background: this.context.color}}>Foo</button>
)
}
}
Button.contextTypes = {
color: React.PropTypes.string
};
class ButtonList extends React.Component {
getChildContext() {
return {color: "purple"};
}
}
<ButtonList><Button /></ButtonList>
Defined on instantiation, passed as attributes in JSX
class Hello extends React.Component {
render() {
return (
<p>Hello {this.props.name}</p>
)
}
}
Hello.propTypes = {
name: React.PropTypes.string
};
ReactDOM.render(<Hello name="Guyllaume" />, rootNode);
Only a few lifecycles to be aware of:
A total of 10 methods to learn about!
Pretty easy, right?
class UserProfileSummary extends React.Component {
render() {
return (
<div>
<ProfilePicture uri={this.props.profilePictureUri} />
<UserInformation
name={this.props.user.name}
memberSince={this.props.user.memberSince}
/>
</div>
);
}
}
Question: What does this component do?
import * as React from 'react'
class RainbowButton extends React.Component {
colors = ['blue', 'red', 'yellow']
constructor(props) {
super(props)
this.state = { currentColor: 'blue' }
}
shouldComponentUpdate(nextProps, nextState) {
return nextState.currentColor !== this.state.currentColor
}
render() {
return (
<button
color={this.state.currentColor}
onClick={() => this.selectRandomColor()}
>
I am a {this.state.currentColor} button!
</button>
);
}
}
React is FAST
The Virtual DOM
React has no solid guidelines on, well, anything
You want your logic to be handled by AngularJS? Sure why not?
You want a wrapper component to handle your authentication? Go ahead.
describe('<ProductRow />', () => {
it('renders with default styling', () => {
const wrapper = shallow(<ProductRow />);
expect(wrapper.find('.out-of-stock')).to.have.length(0);
});
it('renders with out of stock styling', () => {
const wrapper = shallow(<ProductRow />);
expect(wrapper.find('.out-of-stock')).to.have.length(1);
});
});
Your current tools still work (Jasmine, Mocha, Karma, Sinon, etc.)
Shallow Rendering makes it even easier
Edit your components in real time. Keep your application state.
React has no solid guidelines on, well, anything
Ok, but how do I handle routing? Don't even care
Where should I handle AJAX calls? Don't even care
How do I integrate non-React libraries like Pickaday? Don't even care
How would I integrate React into Meteor or AngularJS? Don't. Even. Care.
One of React's strength is also what makes it hard to adopt. There are no guidelines on anything.
It's not a framework.
React is radically different than most frontend frameworks. Wrapping your head around it and thinking in React will take a while.
You can use it however you want. This works:
var counter = 0;
ReactDOM.render(<Counter count={counter} />, rootNode);
setInterval(function() {
counter++;
ReactDOM.render(<Counter count={counter} />, rootNode);
}, 1000);
class HelloWorld extends React.Component {
render() {
return <div>Hello {this.props.name}!</div>
}
}
angular.directive('hello', function(){
return{
link: function(scope, el, attrs){
scope.$watch('name', function(newValue, oldValue){
ReactDOM.render(<HelloWorld name={newValue} />, el[0]);
})
}
}
})
This also works:
All you need to do, is pass data to React components. The rest? You do it the way you want.
This is really the hardest part of using React: figuring out what your application will use and how.
I personally like Redux, React-Router and InversifyJS to control my data.
(At least, some of it)
Rami Sayar did a really good and exhaustive talk on this: https://vimeo.com/189677855
I highly recommend it!
My personal preferences are:
There are a lot of libraries, I recommend looking for "react best practices 2017" on Google and reading a bit to see what's being done.
And there's a LOT more. See this for a smorgasbord of resources:
If you want to find me:
Email: gc@majisti.com
LinkedIn: Guyllaume Cardinal
Github: @gCardinal