Code Monkey home page Code Monkey logo

training_2017_04_12's Introduction

-------------------------------------------------------------------------------
---------------------------Containers and Components---------------------------
-------------------------------------------------------------------------------

Lets start with connect()

connect() takes 2 args and returns a function that takes a single arg

which means this...

export default connect(mapState, mapActions)(MyComponent);

is the same as this...

const preloadedConnect = connect(mapState, mapActions);
export default preloadedConnect(MyComponent);

Separate into files...

components/MyComponent.js

class MyComponent extends Component {
  static propTypes = {
    firstName: PropTypes.string,
    lastName: PropTypes.string,
    onSave: PropTypes.string,
  };
  render() {
    ...
  }
}
export default MyComponent;

containers/MyContainer.js

const mapState = ...;
const mapActions = ...;
export default connect(mapState, mapActions);

Ok... but how do i use it...???

pages/MyPage.js

import MyContainer from '../containers/MyContainer';
import MyComponent from '../component/MyComponent';
const ConnectedComponent = MyContainer(MyComponent);
...
...
render() {
  return (
    <ConnectedComponent />
  );
}

So... why...???

"Dope software brah"

  • Gentry
describe('<MyComponent />', () => {
  it('should be unit tested', () => {
    const wrapper = mount(
      <MyComponent
        firstName="Alex"
        lastName="Either"
        onSave={() => console.log('dead to me')}
      />
    );
    expect(...).to.work.correctly;
  });
});


     _   _ _   _ _____ _____
    | | | | \ | |_   _|_   _|
    | | | |  \| | | |   | |
    | | | | . ` | | |   | |
    | |_| | |\  |_| |_  | |
     \___/\_| \_/\___/  \_/
     _____ _____ _____ _____ _____
    |_   _|  ___/  ___|_   _/  ___|
      | | | |__ \ `--.  | | \ `--.
      | | |  __| `--. \ | |  `--. \
      | | | |___/\__/ / | | /\__/ /
      \_/ \____/\____/  \_/ \____/
    ______ _____ _    _
    |  ___|_   _| |  | |
    | |_    | | | |  | |
    |  _|   | | | |/\| |
    | |     | | \  /\  /
    \_|     \_/  \/  \/


-------------------------------------------------------------------------------
------------------------------React Lifecycle----------------------------------
-------------------------------------------------------------------------------

Phases

  1. Initial Creation
  2. Updates
  3. Unmounting

Initial Creation

  1. constructor()
    • setup state and initial props
  2. componentWillMount()
  3. render()
  4. ref callbacks
  5. componentDidMount()
    • make api calls here
    • setup any listeners / render loops

Updates(state)

  1. setState
  2. shouldComponentUpdate(nextProps, nextState)
    • default returns true
    • ez perf gainz ftw
  3. componentWillUpdate(nextProps, nextState)
  4. render()
  5. ref callbacks
  6. componentDidUpdate(prevProps, prevState)

Updates(props)

  1. props from parent component
  2. componentWillRecieveProps(nextProps)
  3. shouldComponentUpdate(nextProps, nextState)
    • default returns true
    • ez perf gainz ftw
  4. componentWillUpdate(nextProps, nextState)
  5. render()
  6. ref callbacks
  7. componentDidUpdate(prevProps, prevState)

Unmounting

  1. component gets removed from render tree
  2. componentWillUnmount()
    • disconnect all event listeners / render loops

Render perf antipatterns

creating new functions in render (returning an arrow function)

onClick() {
    return () => this.setState();
}
render() {
    return (
        <MyComponent
            onClick={this.onClick()}
        />
    );
}

instead...

onClick = () => {
    this.setState();
}
render() {
    return (
        <MyComponent
            onClick={this.onClick}
        />
    );
}

Render perf antipatterns

creating new objects in render (the terrible {{)

render() {
    return (
        <MyComponent
            things={{
                text: 'Do Something',
                onClick: this.doSomething,
            }}
        />
    );
}

instead...

things = {
    text: 'Do Something',
    onClick: this.doSomething,
}
render() {
    return (
        <MyComponent
            things={this.things}
        />
    );
}

Render perf antipatterns

creating new objects in render (the terrible || {})

render() {
    return (
        <MyComponent
            things={this.props.thing || {}}
        />
    );
}

instead...

defaultThing = {}
render() {
    return (
        <MyComponent
            things={this.props.thing || this.defaultThing}
        />
    );
}

Reference Material

training_2017_04_12's People

Contributors

kevindurb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.