Skip to content

Instantly share code, notes, and snippets.

@ddewaele
Last active February 6, 2018 19:15
Show Gist options
  • Save ddewaele/1c40e6f84e52937b292e2f2d724e110d to your computer and use it in GitHub Desktop.
Save ddewaele/1c40e6f84e52937b292e2f2d724e110d to your computer and use it in GitHub Desktop.

Revisions

  1. ddewaele revised this gist Feb 6, 2018. No changes.
  2. ddewaele revised this gist Feb 6, 2018. 1 changed file with 45 additions and 0 deletions.
    45 changes: 45 additions & 0 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -249,3 +249,48 @@ newState = {...state,age:40}
    ```


    # Decorators

    ## Without a decorator
    ```
    import React from 'react';
    import * as actionCreators from './actionCreators';
    import { bindActionCreators } from 'redux';
    import { connect } from 'react-redux';
    function mapStateToProps(state) {
    return { todos: state.todos };
    }
    function mapDispatchToProps(dispatch) {
    return { actions: bindActionCreators(actionCreators, dispatch) };
    }
    class MyApp extends React.Component {
    // ...define your main app here
    }
    export default connect(mapStateToProps, mapDispatchToProps)(MyApp);
    ```

    ## Using a decorator

    ```
    import React from 'react';
    import * as actionCreators from './actionCreators';
    import { bindActionCreators } from 'redux';
    import { connect } from 'react-redux';
    function mapStateToProps(state) {
    return { todos: state.todos };
    }
    function mapDispatchToProps(dispatch) {
    return { actions: bindActionCreators(actionCreators, dispatch) };
    }
    @connect(mapStateToProps, mapDispatchToProps)
    export default class MyApp extends React.Component {
    // ...define your main app here
    }
    ```
  3. ddewaele revised this gist Nov 7, 2017. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,6 @@ Use [Facebook Create App](https://github.com/facebookincubator/create-react-app)

    Very simple app (in CodePen : https://codepen.io/anon/pen/JOjevM)
    ```
    import React from 'react'
    import ReactDOM from 'react-dom';
    const App = () => (
    <div>
    <h1>Cool App</h1>
    @@ -242,4 +238,14 @@ And there's also a named export called history

    ```
    export const history = createHistory()
    ```
    ```


    # Immutability

    ```
    state = {name:"Davy", age:39}
    newState = {...state,age:40}
    ```


  4. ddewaele revised this gist Nov 7, 2017. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,10 @@ Use [Facebook Create App](https://github.com/facebookincubator/create-react-app)

    Very simple app (in CodePen : https://codepen.io/anon/pen/JOjevM)
    ```
    import React from 'react'
    import ReactDOM from 'react-dom';
    const App = () => (
    <div>
    <h1>Cool App</h1>
    @@ -11,6 +15,13 @@ const App = () => (
    ReactDOM.render(<App/>,document.getElementById("app"));
    ```

    or
    ```
    import React from 'react'
    import { render } from 'react-dom'
    render(<App/>,document.getElementById("app"));
    ```

    ## Components

  5. ddewaele revised this gist Nov 4, 2017. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -200,3 +200,35 @@ const MasterDetail = () => (

    The MasterDetail is capable of rendering 2 components. Master or Detail, depending on the path.




    # Imports

    There are different ways to import files in react.

    https://stackoverflow.com/questions/36795819/when-should-i-use-curly-braces-for-es6-import/36796281

    example
    ```
    import store, { history } from './store'
    ```
    Means there is a default export that we're assigning to `store`

    ```
    //store.js
    ....
    export default createStore(
    rootReducer,
    initialState,
    composedEnhancers
    )
    ```

    And there's also a named export called history

    ```
    export const history = createHistory()
    ```
  6. ddewaele revised this gist Oct 30, 2017. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ equals
    ```
    class Home extends React.Component {
    render () {
    return(
    return (
    <div>
    <h1>Home</h1>
    </div>
    @@ -50,6 +50,19 @@ var Home = React.createClass({
    });
    ```

    ### Little bit more complex

    ```
    const Detail = props => {
      console.log("Place to execute JS logic before rendering");
    return (
    <div>
    <h1>Home</h1>
    </div>
    )
    }
    ```

    ## APIs

    Creating an API
  7. ddewaele revised this gist Oct 30, 2017. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,44 @@ ReactDOM.render(<App/>,document.getElementById("app"));
    ```


    ## Components

    ### Simplest

    ```
    const Home = () => (
    <div>
    <h1>Home</h1>
    </div>
    );
    ```

    equals

    ```
    class Home extends React.Component {
    render () {
    return(
    <div>
    <h1>Home</h1>
    </div>
    )
    }
    }
    ```
    ancient :
    ```
    var Home = React.createClass({
    render: function () {
    return (
    <div>
    <h1>Home</h1>
    </div>
    );
    }
    });
    ```

    ## APIs

    Creating an API
  8. ddewaele revised this gist Oct 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -78,7 +78,7 @@ Using the component:
    ReactDOM.render(
    <Detail player={PlayerAPI.get(1)} />,
    document.getElementById("app"));```
    ```


    # React Router
  9. ddewaele revised this gist Oct 29, 2017. 1 changed file with 53 additions and 2 deletions.
    55 changes: 53 additions & 2 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -83,18 +83,69 @@ ReactDOM.render(
    # React Router
    You need to import some stuff :
    ```
    import React from 'react'
    import { render } from 'react-dom'
    import { BrowserRouter } from 'react-router-dom'
    import App from './components/App';
    ```
    or
    ```
    const { BrowserRouter, Switch, Route, Link } = ReactRouterDOM;
    ```
    and code:
    ```
    render((
    <BrowserRouter>
    <App />
    </BrowserRouter>
    ), document.getElementById('root'));
    ```
    ```
    Your main app should define the top level routes
    ```
    const App = () => (
    <div>
    <Header />
    <main>
    <Switch>
    <Route exact path="/" component={Home} />
    <Route path="/master-detail" component={MasterDetail} />
    <Route path="/about" component={About} />
    </Switch>
    </main>
    </div>
    );
    ```
    Our header section simply displays the links to the routes
    ```
    const Header = () => (
    <header>
    <Link to="/">Home</Link>
    <Link to="/master-detail">MasterDetail</Link>
    <Link to="/about">About</Link>
    </header>
    );
    ```
    When we click a link, a certain route is activated and the assoicated component is rendered.
    When clicking masterdetail link, the MasterDetail component is rendered:
    ```
    const MasterDetail = () => (
    <Switch>
    <Route exact path='/master-detail' component={Master}/>
    <Route path='/master-detail/:number' component={Detail}/>
    </Switch>
    );
    ```
    The MasterDetail is capable of rendering 2 components. Master or Detail, depending on the path.
  10. ddewaele revised this gist Oct 29, 2017. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -77,4 +77,24 @@ Using the component:
    ```
    ReactDOM.render(
    <Detail player={PlayerAPI.get(1)} />,
    document.getElementById("app"));```
    document.getElementById("app"));```
    # React Router
    ```
    import React from 'react'
    import { render } from 'react-dom'
    import { BrowserRouter } from 'react-router-dom'
    import App from './components/App';


    render((
    <BrowserRouter>
    <App />
    </BrowserRouter>
    ), document.getElementById('root'));
    ```
  11. ddewaele revised this gist Oct 29, 2017. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,15 @@ const App = () => (
    ```

    Other component

    ```
    const Detail = (props) => (
    <div>
    {props.player.name}
    </div>
    );
    ```
    or shorter:
    ```
    const Detail = ({player}) => (
    <div>
  12. ddewaele revised this gist Oct 29, 2017. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -67,5 +67,6 @@ const Detail = ({player}) => (
    Using the component:

    ```
    ReactDOM.render(<Detail player={PlayerAPI.get(1)} />, document.getElementById("app"));
    ```
    ReactDOM.render(
    <Detail player={PlayerAPI.get(1)} />,
    document.getElementById("app"));```
  13. ddewaele revised this gist Oct 29, 2017. 1 changed file with 59 additions and 0 deletions.
    59 changes: 59 additions & 0 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,63 @@ const App = () => (
    )
    ReactDOM.render(<App/>,document.getElementById("app"));
    ```


    ## APIs

    Creating an API

    ```
    const PlayerAPI = {
    players: [
    { number: 1, name: "Ben Blocker", position: "G" },
    { number: 2, name: "Dave Defender", position: "D" },
    { number: 3, name: "Sam Sweeper", position: "D" },
    { number: 4, name: "Matt Midfielder", position: "M" },
    { number: 5, name: "William Winger", position: "M" },
    { number: 6, name: "Fillipe Forward", position: "F" }
    ],
    all: function() {
    return this.players;
    },
    get: function(id) {
    const isPlayer = p => p.number === id;
    return this.players.find(isPlayer);
    }
    };
    ```

    Using the API

    ```
    const App = () => (
    <div>
    <h1>Cool App</h1>
    <ul>
    {PlayerAPI.all().map(p => (
    <li key={p.number}>
    <div>{p.name}</div>
    </li>
    ))}
    </ul>
    </div>
    );
    ```

    Other component

    ```
    const Detail = ({player}) => (
    <div>
    {player.name}
    </div>
    );
    ```

    Using the component:

    ```
    ReactDOM.render(<Detail player={PlayerAPI.get(1)} />, document.getElementById("app"));
    ```
  14. ddewaele revised this gist Oct 29, 2017. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,12 @@
    Use [Facebook Create App](https://github.com/facebookincubator/create-react-app)

    Very simple app (in CodePen : https://codepen.io/anon/pen/JOjevM)
    ```
    const App = () => (
    <div>
    <h1>Cool App</h1>
    </div>
    )
    ReactDOM.render(<App/>,document.getElementById("app"));
    ```
  15. ddewaele created this gist Oct 29, 2017.
    2 changes: 2 additions & 0 deletions reactjs-notes.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    Use [Facebook Create App](https://github.com/facebookincubator/create-react-app)