Tom Panier
Web developer (Goodays) • Musician (Cold Phase, Ubik) • Far-left ranter
REACT
ChtiJS #10 - 5/02/2015
// helloWorld.js
window.HelloWorld = React.createClass({
render: function() {
return React.createElement(
'div',
{ className: 'hello-world' },
'Hello world!'
);
}
});
<div id="content"></div>
<script src="helloWorld.js"></script>
<script>
React.render(
React.createElement(window.HelloWorld),
document.getElementById('content')
);
</script>
var HelloWorld = React.createClass({
render: function() {
return (
<div className="hello-world">Hello world!</div>
);
}
});
React.render(
React.createElement(HelloWorld),
document.getElementById('content')
);
Du HTML
dans le JS ?
Mais c'est dég...
LOL
NOOB
var gulp = require('gulp'),
env = require('gulp-env'),
browserify = require('gulp-browserify');
env({ vars: { NODE_ENV: 'production' } }); // ou 'development'
gulp.task('jsx', function () {
gulp.src('path/to/app.js')
.pipe(browserify({
insertGlobals: false,
transform: ['reactify'],
extensions: ['.jsx']
}))
.pipe(gulp.dest('path/to/compiled/'));
});
npm install react gulp-browserify reactify gulp-env --save-dev
var HelloWorld = React.createClass({
render: function() {
return (
['div', { className: 'hello-world' }, 'Hello world!']
);
}
});
React.render(
React.createElement(HelloWorld),
document.getElementById('content')
);
var HelloWorld = React.createClass({
render: function() {
return (
<div className="hello-world">
Hello {this.props.name}!
</div>
);
}
});
React.render(
React.createElement(HelloWorld, { name: 'Tom' }),
document.getElementById('content')
);
Oscar de la meilleure blague 1962
var SuperUsefulApp = React.createClass({
render: function() {
return (
<HelloWorld name="Tom" />
);
}
});
React.render(
React.createElement(SuperUsefulApp),
document.getElementById('content')
);
var SuperUsefulApp = React.createClass({
getInitialState: function() {
return { name: '' };
},
handleInput: function(event) {
this.setState({ name: event.target.value });
},
render: function() {
return (
<div className="super-useful-app">
<input type="text" onInput={this.handleInput} />
<HelloWorld name={this.state.name} />
</div>
);
}
});
Un changement rappelle (reactive programming)
render()
Oscar de la meilleure blague 2006
var SuperUsefulApp = React.createClass({
getInitialState: function() {
return { name: '' };
},
fetchNameFromServer: function() {
return new Promise(function (resolve, reject) {
taLibAjaxFavorite({
url: this.props.url,
method: 'GET',
success: function(name) {
resolve(name);
}
});
}.bind(this));
},
updateName: function() {
this.fetchNameFromServer()
.then(function (name) {
this.setState({ name: name });
}.bind(this));
},
componentWillMount: function() {
// on récupère le nom tout de suite
this.updateName();
// on le met à jour toutes les secondes
// ça serait mieux avec une websocket (vrai realtime)
this.setInterval(this.updateName, 1000);
},
render: function() {
return (
<HelloWorld name={this.state.name} />
);
}
});
render: function() {
return (
<div class="super-useful-app">
<input type="text"
onInput={this.handleInput}
ref="input" />
<HelloWorld name={this.state.name} />
</div>
);
}
var testUtils = React.addons.TestUtils,
testApp = testUtils.renderIntoDocument(<SuperUsefulApp />),
input = testApp.refs.input.getDOMNode();
expect(input.value).to.equal('');
testUtils.Simulate.click(input);
testUtils.Simulate.change(input, { target: { value: 'Johnny' } });
testUtils.Simulate.keyDown(input, { key: 'Enter' });
describe('Components.HelloWorld', function() {
this.reactSuite({ type: HelloWorld });
it('should render', function() {
expect(subject.isMounted()).toBeTruthy();
});
});
var testApp = new PageObject(<SuperUsefulApp />);
expect(testApp.input.value).to.equal('');
testApp.input.click();
testApp.input.value = 'Johnny';
testApp.input.keyDown('Enter');
$('button').on('click', function () { /* ... */ });
var nom = prompt('Quel est votre nom ?');
var SomeClass = function() {};
SomeClass.prototype.sayCheese = function() {
return 'Cheese!';
};
var instance = new SomeClass();
console.log(instance.sayCheese());
Et c'est
pas fin...
TG
Des questions ?
By Tom Panier
ChtiJS #10 - 5/02/2015