import React, { Component } from 'react';
import { render } from 'react-dom';
class App extends Component {
constructor() {
super(...arguments);
this.state = {
thisCan: 'be whatever you want'
};
this.setState({
and: 'can be managed and abstracted',
however: 'you wish'
});
}
render() {
return (
<div>
Hello World
{this.state.thisCan}
</div>
);
}
}
render(<App />, document.getElementById('app'));
import React, { Component } from 'react';
import { render } from 'react-dom';
class App extends Component {
constructor(...args) {
super(...args);
this.state = {
currentCount: 0
};
setInterval(() => {
this.setState({
currentCount: this.state.currentCount + 1
});
//Every time setState is called,
//React diff's the changed state and fires off a DOM update!
}, 100);
}
render() {
const { currentCount } = this.state;
return (
<div>Count: {currentCount}</div>
);
}
}
render(<App />, document.getElementById('app'));
import React, { Component } from 'react';
import { render } from 'react-dom';
class App extends Component {
render() {
return (
<div>
Hello World
<span className='container'>
Goodbye World
</span>
</div>
);
}
}
render(<App />, document.getElementById('app'));
import React, { Component } from 'react';
import { render } from 'react-dom';
class SubComponent extends Component {
render() {
return (
<div>
I am a sub component!
</div>
);
}
}
const subComponent = <SubComponent />;
class App extends Component {
render() {
return (
<div>
You always need a wrapper element!
<SubComponent /> { /* Comments in JSX need to be within braces */ }
{ /* Components need to have the first letter capitalized! */ }
{ /* <subComponent /> will not work! */ }
{subComponent /* This works because we're rendering a variable */}
</div>
);
}
}
render(<App />, document.getElementById('app'));
26208 packages found for "react" on npm
Backed by numerous large companies, notably Facebook
Just install...
const path = require('path');
const BUILD_DIR = path.resolve(__dirname, '../server/static/');
const APP_DIR = path.resolve(__dirname, './app');
const TARGET = process.env.npm_lifecycle_event;
process.env.BABEL_ENV = TARGET;
const config = {
entry: [
APP_DIR + '/index.jsx'
],
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel'
},
{
test: /\.(png|jpg)$/,
include : APP_DIR,
loader: 'url-loader?limit=8192'
},
{
test: /\.js$/,
include: APP_DIR,
loader: 'babel'
},
{
test: /\.json$/,
include: APP_DIR,
loader: 'babel'
}
],
},
resolve: {
extensions: [
'',
'.js',
'.jsx',
'.css',
'.scss'
]
}
};
module.exports = config;
webpack.config.js
class A extends Component {
constructor(...args) {
super(...args);
this.state = {
a: 'b'
}
}
}
class B extends Component {
constructor(...args) {
super(...args);
this.state = {
b: 'a'
}
}
}
State managed per-component
Requires external libraries to sync across components
class A extends Component {
constructor(...args) {
super(...args);
this.state = {
a: 'b'
}
}
render() {
return <B a={this.state.a} />
}
}
class B extends Component {
constructor(...args) {
super(...args);
this.state = {
b: this.props.a || 'a'
}
}
}
Functional Components
class A extends Component {
constructor(...args) {
super(...args);
this.state = {
a: 'b'
}
}
render() {
return <B a={this.state.a} />
}
}
const B = function(props) {
return (
<div>{props.a || 'a'}</div>
);
}
class Hello extends React.Component {
render() {
return (
<div>
Hello {this.props.toWhat}
</div>
);
}
}
class Hello extends React.Component {
render() {
return React.createElement(
'div',
null,
`Hello ${this.props.toWhat}`
);
}
}
You're writing Javascript.
Your team is writing Javascript.
React isn't picky about what you do, and....
Most people suck at writing Javascript.
Shameless Plug, Redux Black Magic:
https://github.com/krishnaglick/redux-black-magic
/krishnaglick on Github
@krishnaglick on Twitter
@prometheus on ODevs Slack