Giter Club home page Giter Club logo

Comments (10)

mweststrate avatar mweststrate commented on July 16, 2024 1

from mobx-react.

iiagodias avatar iiagodias commented on July 16, 2024 1

I found the solution, you just have to declare the action functions as follows:

toggleText = () => {
    this.textVisibility = !this.textVisibility;
}

Final:

class TestStore {
  textVisibility = false;

  constructor() {
    makeObservable(this, {
      textVisibility: observable,
      isTextVisible: computed,
      toggleText: action
    });
  }

  get isTextVisible() {
    return this.textVisibility;
  }

  toggleText = () => {
    this.textVisibility = !this.textVisibility;
  }
}

const testStore = new TestStore();

from mobx-react.

saeid85 avatar saeid85 commented on July 16, 2024

Thanks for the response.
Actually this is not the real code & I just publish a sample code to demonstrate what I mean. In the real project I use typechecker.

from mobx-react.

skillful-alex avatar skillful-alex commented on July 16, 2024

I have a similar question, so I decided not to open a new issue, but to ask in this one.

I am trying to create a react application using mobx and typescript. But it doesn't work.

I expect the timer to count the seconds. And I see that the event happens and updates the counter. But the component is not rerender. What am I doing wrong?

import React from "react";
import { observable, action } from "mobx";
import { observer, inject, Provider } from "mobx-react";

export class TestStore {
    @observable timer = 0;

    @action timerInc = () => {
        this.timer += 1;
    };
}

interface IPropsTestComp {
    TestStore?: TestStore;
}

@inject("TestStore")
@observer
export class TestComp extends React.Component<IPropsTestComp> {
    constructor(props: IPropsTestComp) {
        super(props);
        setInterval(() => {
            this.props.TestStore!.timerInc();
        }, 1000);
    }

    render() {
        return <div>{this.props.TestStore!.timer}</div>;
    }
}

export class TestApp extends React.Component {
    render() {
        return <Provider TestStore={new TestStore()}>
            <TestComp />
        </Provider>
    }
}

"mobx": "^6.0.1",
"mobx-react": "^7.0.0",
"react": "^16.13.1",

Updated:
I was helped on the stackoverflow . Working code:

import { makeAutoObservable } from "mobx";

export class TestStore {
    timer = 0;

    constructor() {
      // Don't need decorators now, just this call
      makeAutoObservable(this);
    }

    timerInc = () => {
        this.timer += 1;
    };
}

from mobx-react.

mweststrate avatar mweststrate commented on July 16, 2024

from mobx-react.

saeid85 avatar saeid85 commented on July 16, 2024

@skillful-alex Mobx 6 works fine with React, but when it comes to React Native.... My issue is how to use Mobx6 in a React Native project!

from mobx-react.

mweststrate avatar mweststrate commented on July 16, 2024

React versus React Native should be quite irrelevant from MobX perspective. But without any more accurate information / minimal reproduction we cannot be of any further assistance.

from mobx-react.

iiagodias avatar iiagodias commented on July 16, 2024

same problem

from mobx-react.

iiagodias avatar iiagodias commented on July 16, 2024

As I understand it, this is the native version of React.

from mobx-react.

mweststrate avatar mweststrate commented on July 16, 2024

same solution

from mobx-react.

Related Issues (20)

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.