Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does the react-hooks not support functions? #72

Open
HYEOK999 opened this issue Apr 9, 2020 · 0 comments
Open

Does the react-hooks not support functions? #72

HYEOK999 opened this issue Apr 9, 2020 · 0 comments

Comments

@HYEOK999
Copy link

HYEOK999 commented Apr 9, 2020

Hello I am a student studying React.
Recently, I am studying React Mobx and I want to use React Hooks. It seems to work well in class form, but does it come out strangely in Hooks? Is this right?

I will also leave the sauce below.

// Class Component 
import React, { Component } from 'react';
import { decorate, observable, action } from 'mobx';
import { observer } from 'mobx-react';

class Counter extends Component {
  number = 0;

  increase = () => {
    this.number++;
  };

  decrease = () => {
    this.number--;
  };

  render() {
    return (
      <div>
        <h1>{this.number}</h1>
        <button onClick={this.increase}>+1</button>
        <button onClick={this.decrease}>-1</button>
      </div>
    );
  }
}

decorate(Counter, {
  number: observable,
  increase: action,
  decrease: action,
});

export default observer(Counter);
// React Hooks
import React from 'react';
import { useObserver, useLocalStore } from 'mobx-react';

const Counter2 = (props) => {
  const store = useLocalStore(() => ({
    number: 0,
    increase() {
      store.number++;
    },
    decrease() {
      store.number++;
    },
  }));

  return useObserver(() => (
    <div>
      <h1>{store.number}</h1>
      <button onClick={store.increase}>+1</button>
      <button onClick={store.decrease}>-1</button>
    </div>
  ));
};

export default Counter2;

11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant