Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Počni da koristiš React odmah

DaFED
September 10, 2015

Počni da koristiš React odmah

DaFED#36
Speaker: Goran Gajić
React je biblioteka za pravljenje korisničkih interfejsa napravljena od strane Facebook-a. Možemo o njoj da mislimo kao o "V" (view) u MVC arhitekturi. React se koristi za pravljenje i ažuriranje kompleksnih korisničkih interfejsa i namena mu je da se koristi pored drugih popularnih biblioteka (npr. Backbone.js). U ovom predavanju ćete videti kako možete da probate da koristite React u vašim postojećim aplikacijama i sami vidite da li je React za vas ili ne.

DaFED

September 10, 2015
Tweet

More Decks by DaFED

Other Decks in Technology

Transcript

  1. Composable Components Vasa aplikacija napravljena od malih komponenata lako se

    testiraju sadrze jednu drugu jednostavno za koriscenje lakse je praviti promene u postojecem kodu
  2. Virtual DOM React koristi virtual DOM diff implementaciju za veoma

    visoke performanse. Ne pristupate direktno DOM-u nego putem react-a automatski zameni sta se promenilo na najoptimalniji nacin smanjuje reflow i repaint vidljivo povecanje performansi
  3. Primer var ShoppingList = React.createClass({ render: function() { return React.createElement("ul",

    {className: "shopping-list"}, React.createElement("li", null, " Pivo "), React.createElement("li", null, " Smoki "), React.createElement("li", null, " Kikiriki ") ); } }); React.render(React.createElement(ShoppingList, null), mountNode);
  4. JSX JSX var Dafed = React.createClass({ render: function() { return

    ( <h1 className="welcome">Hello DaFED</h1> ); } }); React.render(<Dafed/>, document.getElementById('dafed')); var Dafed = React.createClass({ render: function() { return ( React.createElement("h1", {className: "welcome"}, " Hello DaFED ") ); } }); React.render(React.createElement(Dafed, null), document.getElementById('dafed'));
  5. Zašto JSX? Zašto JSX? Razdvajanje problema, a ne tehnologija komponente

    koristiti samo za ui, a ne i logiku ostale biblioteke stavljaju js u html dok react stavlja html u vas js sve je samo javascript
  6. Primer komponente Primer komponente var PlayersList = React.createClass({ render: function()

    { var players = this.state.players.map(function(player) { return ( <li> <PlayerLink player={player}> <Avatar player={player}/> <Username player={player}/> </PlayerLink> </li> ) }); return <ul className="players-list">{players}</ul> } }); class PlayersList extends React.Component { render() { ... } }
  7. props, state props, state var PlayersList = React.createClass({ getInitialState: function()

    { return { players: PlayersStore.get() }; }, onChange: function() { this.setState({players: PlayersStore.get()}); }, render: function() { var players = this.state.players.map(function(player) { return ( <li> <PlayerLink player={player}> <Avatar player={player}/> <Username player={player}/> </PlayerLink> </li> ) }); return <ul className="players-list">{players}</ul> } });
  8. componentDidMount, componentWillUnmount componentDidMount, componentWillUnmount var PlayersList = React.createClass({ componentDidMount() {

    PlayerStore.addChangeListener(this.onChange); }, componentWillUnmount() { PlayerStore.removeChangeListener(this.onChange); }, getInitialState: function() { return { players: PlayersStore.get() }; }, onChange: function() { this.setState({players: PlayersStore.get()}); }, render: function() { var players = this.state.players.map( ... return <ul className="players-list">{players}</ul> } });
  9. Event System Event System var PlayersList = React.createClass({ getInitalState: function()

    { return { open: false } }, toggleModal: function() { this.setState({open: !this.state.open}); }, render: function() { if (this.state.open) { return <BootstrapModal /> } return <BootstrapButton type="primary" onClick={this.toggleModal}/>; } });
  10. React implementira jednosmerni reactivni protok podataka sto smanjuje kod i

    lakse je razmisljati na taj nacin nego tradicionalni data binding Flux Flux
  11. Backbone View => React Component Backbone View => React Component

    var React = require('react'); var PlayersOnlineView = require('views/homepage/playersOnline'); var PlayersOnline = React.createClass({ shouldComponentUpdate: function() { return false; }, componentDidMount() { var parent = React.findDOMNode(this.refs.parent); var element = document.createElement("DIV"); parent.appendChild(element); this.playersOnline = new PlayersOnlineView({ el: element }); }, componentWillUnmount() { this.playersOnline.remove(); }, render() { return <div className="players-online" ref="parent"></div>; } });