GithubHelp home page GithubHelp logo

anti-redux's Introduction

Anti Redux | Context API (store)

Creating the store

import React from "react";

const Store = React.createContext(null);

export default Store;

Consuming the store

Save data

<Store.Provider value={data}></> : store 에 data 저장

import Store from "store";

class Container extends Component {
  state = {
    message: "Hello"
  };
  // store 에 저장된 state 는 변경될때 마다 사용 위치에 적용됨
  componentDidMount = () => {
    setTimeout(() => {
      this.setState({
        message: "bye"
      });
    }, 2000);
  };
  render() {
    // Store 에 value 의 data 저장
    return (
      <Store.Provider value={this.state}>
        <AppPresenter />
      </Store.Provider>
    );
  }
}

Use data

<Store.Consumer>{store => ()</> : store의 props 에 접근

import Store from "store";

const Presenter = () => {
  <Title>
    <Store.Consumer>{store => store.message}</Store.Consumer>
  </Title>;
};

Updating the store

Method to send must be in constructor

Provider.js

class Container extends Component {
  constructor(props) {
    super(props);
    this._changeMsg = () => {
      if (this.state.message === "Hello") {
        this.setState({
          message: "Bye"
        });
      } else {
        this.setState({
          message: "Hello"
        });
      }
    };
    this.state = {
      message: "Hello",
      changeMsg: this._changeMsg
    };
  }
  render() {
    return (
      <Store.Provider value={this.state}>
        <AppPresenter />
      </Store.Provider>
    );
  }
}

Consumer.js

const Presenter = () => (
  <Fragment>
    <Store.Consumer>
      {store => <Button success seen={seen} onClick={store.changeMsg} />}
    </Store.Consumer>
  </Fragment>
);

anti-redux's People

Contributors

kwon770 avatar dependabot[bot] avatar

Stargazers

 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.