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

Getting Started with ReactJs & Ecosystem

Getting Started with ReactJs & Ecosystem

Sastra Nababan

March 25, 2017
Tweet

More Decks by Sastra Nababan

Other Decks in Programming

Transcript

  1. and…. • Declarative • Modular • Component Based • One-way

    data flow • Virtual DOM • Learn Once, Write Anywhere
  2. Who use ReactJS ? • Facebook • Instagram • NetFlix

    • Khan Academy • Twitter • Hootsite • Yahoo • wordpress.com • and over 1000 company, app, agencies…..
 
 IUUQTHJUIVCDPNGBDFCPPLSFBDUXJLJTJUFTVTJOHSFBDU
  3. We need to go 5 or 10 years back to

    understand the fear of developing in JavaScript.
  4. Now the server sends only raw unformatted data and the

    client is responsible for generating the screen out of it. Client Side MVC*
  5. Before you learn react make sure you familiar with :

    Intermediate Javascript :
 Closures, Callbacks, this, map,filter,reduce,object ES6 :
 • object literal and template strings • Block scopes and let/const vs var • Arrow functions • Destructuring and default/rest/spread. • Classes and inheritance • Imports and exports of modules
  6. function MyComponent(props){ return (<div> <p>This is my Component </p></div>) }

    ReactDOM.render( <MyComponent/>,container) How to Create Component Now you can render it It’s Just a simple function
  7. function MyComponent(props){ return (<div> <p>This is my Component called :

    {props.name} </p>
 </div>) ReactDOM.render( <MyComponent name=“bodat”/>,container) How to Create Component It’s Just a simple function Now you can render it
  8. function MyComponent(props){ return (<div> <p OnClick={props.onAction}>This is my Component called

    : {props.name} </p></div>) var myAction=function(){
 alert(“Oh I’m clicked”)
 } ReactDOM.render( <MyComponent name=“bodat” onAction={myAction}
 How to Create Component It’s Just a simple function Now you can render it
  9. class MyComponent extends React.Component{ constructor(props){ super(props) } render(){ return (<div>

    <p onClick={this.props.clickAction}> This is my Component called :{this.props.name} </p> </div>) var myAction=function(){
 alert(“Oh I’m clicked”)
 } ReactDOM.render( <MyComponent name=“bodat” clickAction={myAction}
 How to Create Component Class Component Now you can render it
  10. function Welcome(props) { return <h1>Hello, {props.name}</h1>; } function App() {

    return ( <div> <Welcome name="Sara" /> <Welcome name="Cahal" /> <Welcome name="Edite" /> </div> ); } ReactDOM.render( <App />, document.getElementById('root') ); Composing Component Parent - Child Component
  11. What about State ? Prop is read-only 
 Use State

    to update component or dynamic change
  12. Login Status <header>
 <div class=“name”> Not Logged in </div>
 </header>

    $.post(‘/login’,credential,function(user){ // modify the DOM here
 $(‘header . name”).text(user.name) }
 html jquery
  13. class Button extends React.Component{ constructor(props){ super(props); this.state={clicks : 0} this.count=this.count.bind(this);

    } count(){ this.setState({ clicks : this.state.clicks + 1 }); } render(){ return (<div> <button onClick={this.count}> {this.props.caption}</ button> <p> Button Clicked : {this.state.clicks} times </p> </div>) } } Button Counter Example
  14. render(){ return (<header> {this.state.name ? this.state.name : <span> Not Logged

    in </span> </header> } Login Status <header>
 <div class=“name”> Not Logged in </div>
 </header> html react
  15. class FetchDemo extends React.Component { constructor(props) { super(props); this.state =

    { posts: [] }; } componentDidMount() { axios.get(this.props.url) .then(res => { const posts = res.data.data.children.map(obj => obj.data); this.setState({ posts }); }); } render(){ <div> {this.props.posts} </div> } } ReactDOM.render( <FetchAjax url=“http://yourwebsite/endpoint"/>, document.getElementById('root') ); Fetch Data with ajax
  16. render() { return ( <div> <h1>{`/r/${this.props.subreddit}`}</h1> <ul> {this.state.posts.map(post => <li

    key={post.id}>{post.title}</li> )} </ul> </div> ); } } ReactDOM.render( <FetchDemo subreddit="reactjs"/>, document.getElementById('root') ); Fetch Data with ajax