ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React
class HelloMessage extends React.Component {
render() {
return <div>Hello {this.props.name}</div>;
}
}
ReactDOM.render(<HelloMessage name="Sebastian" />, mountNode);
class App extends Component {
componentWillMount() {
setTimeout(() => {
this.setState({title: '5s elapsed!'})
}, 5000)
}
render() {
return <h1>{this.state.title}</h1>;
}
}
class LifecycleComponent extends React.Component {
componentWillMount() {
console.log('Component WILL MOUNT!')
}
componentDidMount() {
console.log('Component DID MOUNT!')
}
componentWillReceiveProps(newProps) {
console.log('Component WILL RECEIVE PROPS!')
}
componentWillUpdate(nextProps, nextState) {
console.log('Component WILL UPDATE!');
}
componentDidUpdate(prevProps, prevState) {
console.log('Component DID UPDATE!')
}
}
Universal Javascript in React
import ReactDOM from 'react-dom/server';
ReactDOM.renderToString(<HelloMessage name="Sebastian" />);
Universal Javascript in React
import {Router, browserHistory} from 'react-router';
const routes = (
<Route path="/" component={App}>
<IndexRoute component={CharacterListContainer}/>
<Route path=":id" component={CharacterContainer}/>
</Route>
);
ReactDOM.render(<Router routes={routes} history={browserHistory}/>, reactRoot);
import fetch from 'isomorphic-fetch';
const promise = fetch('http://example.com/users');
promise.then(onSuccess, onFailure);
function onSuccess(users) {
console.table(users.json());
}
function onFailure(err) {
console.error(err);
}
Universal Javascript in React
const RootProvider = React.createClass({
render () {
return <ReactRouter.RouterContext {...this.props} />;
}
});
Transmit.renderToString(RootProvider, renderProps).then(({reactString, reactData}) => {
let template = (
`<!doctype html>
<html>
<body>
<div id='react-root'>${reactString}</div>
</body>
</html>`
);
return Transmit.injectIntoMarkup(template, reactData, [`/main.js`]);
});
const StudentDetailsTransmit = Transmit.createContainer(StudentDetails, {
initialVariables: {},
fragments: {
student(variables) {
return Promise.resolve({
name: variables.name
});
}
}
});
Transmit.render(<StudentDetailsTransmit variables={{name: 'kowalski'}}/>, reactRoot);
// Or with React Router
Transmit.render(ReactRouter.Router, {routes: routes, history: ReactRouter.browserHistory}, reactRoot);
Universal Javascript in React
Universal Javascript in React
Universal Javascript in React

More Related Content

Universal Javascript in React

  • 13. class HelloMessage extends React.Component { render() { return <div>Hello {this.props.name}</div>; } } ReactDOM.render(<HelloMessage name="Sebastian" />, mountNode);
  • 14. class App extends Component { componentWillMount() { setTimeout(() => { this.setState({title: '5s elapsed!'}) }, 5000) } render() { return <h1>{this.state.title}</h1>; } }
  • 15. class LifecycleComponent extends React.Component { componentWillMount() { console.log('Component WILL MOUNT!') } componentDidMount() { console.log('Component DID MOUNT!') } componentWillReceiveProps(newProps) { console.log('Component WILL RECEIVE PROPS!') } componentWillUpdate(nextProps, nextState) { console.log('Component WILL UPDATE!'); } componentDidUpdate(prevProps, prevState) { console.log('Component DID UPDATE!') } }
  • 17. import ReactDOM from 'react-dom/server'; ReactDOM.renderToString(<HelloMessage name="Sebastian" />);
  • 19. import {Router, browserHistory} from 'react-router'; const routes = ( <Route path="/" component={App}> <IndexRoute component={CharacterListContainer}/> <Route path=":id" component={CharacterContainer}/> </Route> ); ReactDOM.render(<Router routes={routes} history={browserHistory}/>, reactRoot);
  • 20. import fetch from 'isomorphic-fetch'; const promise = fetch('http://example.com/users'); promise.then(onSuccess, onFailure); function onSuccess(users) { console.table(users.json()); } function onFailure(err) { console.error(err); }
  • 22. const RootProvider = React.createClass({ render () { return <ReactRouter.RouterContext {...this.props} />; } }); Transmit.renderToString(RootProvider, renderProps).then(({reactString, reactData}) => { let template = ( `<!doctype html> <html> <body> <div id='react-root'>${reactString}</div> </body> </html>` ); return Transmit.injectIntoMarkup(template, reactData, [`/main.js`]); });
  • 23. const StudentDetailsTransmit = Transmit.createContainer(StudentDetails, { initialVariables: {}, fragments: { student(variables) { return Promise.resolve({ name: variables.name }); } } }); Transmit.render(<StudentDetailsTransmit variables={{name: 'kowalski'}}/>, reactRoot); // Or with React Router Transmit.render(ReactRouter.Router, {routes: routes, history: ReactRouter.browserHistory}, reactRoot);

Editor's Notes

  • #4: Cz??? 2: m¨®wi o tym, jak si? dzisiaj programuje i jakie s? problemy t?umaczymy czym jest universal i jak to pomog?o netflixowi przypomnienie reacta i dlaczego pozwala na universal Cz??? 3: Opowiadamy o aplikacji i przedstawiamy pierwszy sprint Iteracja 1: Checkout do kroku 1 Napisanie komponentu App.js + hello world na ?rodu ekranu Iteracja 2: Checkout do kroku 2 Dodanie server.js bez transmita (react dom) Iteracja 3: Checkout do kroku 3 Pobieranie danych z marvel api na froncie Wyt?umaczenie dlaczego si? zepsu?o Iteracja 4: Checkout do kroku 4 Pod??czenie transmita Cz??? 4: Podsumowanie i Q&A Cz??? 5: Jeste?my cool, mamy react, es 2015, mamy startup, i inne fajne s?owa
  • #9: Wspomnie? o http2 Nie t?umaczcie ¡°Flash of unstyled content¡± - jak to, nie mog? da? ¡°nag?y nap?yw nieostylowanej tre?ci?¡± NNNT? NIE? SAMI SE R?BCIE TE PREZKE!!!jeden!!
  • #10: Nie t?umaczcie ¡°Flash of unstyled content¡±
  • #15: setState prowadzi do rendera