mobxjs / mobxjs/mobx-devtools

Does the react-hooks not support functions?

Open
#72 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
502
Forks
34
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by reproducing the React Hooks example and comparing it with the class component; the issue does not define a requested code change or a testable done condition, so clarify expected behavior before estimating implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.