mobx

MobX + React Native : way to inject stores

霸气de小男生 提交于 2019-12-22 08:14:17
问题 I'm trying to work with MobX for a new project. I started it on May 2017, and everything was working well. I had to stop, and now I go on developing it. I had to make an npm install to manage making it working, but now, I have some problem with stores... I rely on this tutorial for the structure : https://blog.callstack.io/write-react-native-apps-in-2017-style-with-mobx-e2dffc209fcb This is my structure : Main index.js import { Provider } from 'mobx-react'; import Stack from './router';

MobX autorun behavior

家住魔仙堡 提交于 2019-12-22 05:53:23
问题 I'm exploring MobX and went intrigued by a problem: If I have this observable: class ItemsStore { @observable items = [1,2,3]; } const store = new ItemsStore; and then change it like this: setInterval(() => { store.items[0] = +new Date }, 1000) I noticed the following: autorun(() => console.log(store.items)); never fires... autorun(() => console.log(store.items[0])); fires every 1s and gives a new value autorun(() => console.log(store.items.length)); fires every 1s although value is unchanged

MobX autorun behavior

喜夏-厌秋 提交于 2019-12-22 05:53:07
问题 I'm exploring MobX and went intrigued by a problem: If I have this observable: class ItemsStore { @observable items = [1,2,3]; } const store = new ItemsStore; and then change it like this: setInterval(() => { store.items[0] = +new Date }, 1000) I noticed the following: autorun(() => console.log(store.items)); never fires... autorun(() => console.log(store.items[0])); fires every 1s and gives a new value autorun(() => console.log(store.items.length)); fires every 1s although value is unchanged

Using the MobX @action decorator with async functions and .then

只愿长相守 提交于 2019-12-22 03:22:42
问题 I'm using MobX 2.2.2 to try to mutate state inside an async action. I have MobX's useStrict set to true. @action someAsyncFunction(args) { fetch(`http://localhost:8080/some_url`, { method: 'POST', body: { args } }) .then(res => res.json()) .then(json => this.someStateProperty = json) .catch(error => { throw new Error(error) }); } I get: Error: Error: [mobx] Invariant failed: It is not allowed to create or change state outside an `action` when MobX is in strict mode. Wrap the current method in

How to use mobx in react-native 0.56 (Babel 7) with Decorators

孤街醉人 提交于 2019-12-18 10:54:45
问题 i've upgraded my RN app from 0.55.4 to 0.56 that use Babel 7. In 0.55.4 to use decorators for MOBX i use "babel-plugin-transform-decorators-legacy" but is not compatible with Babel 7... react-native ver: 0.56.0 mobx ver: 5.0.3 mobx-react ver: 5.2.3 does anyone have the solution? Thanks UPDATE: The app works in DEBUG with this configuration package.json ... "devDependencies": { "@babel/core": "7.0.0-beta.47", "@babel/plugin-proposal-decorators": "7.0.0-beta.47" ... } .babelrc { "presets": [ [

Can't call setState on a component that is not yet mounted

可紊 提交于 2019-12-18 05:43:26
问题 this is the first time I face this warning message. Can't call setState on a component that is not yet mounted. Follows: This is a no-op, but it might indicate a bug in your application. Instead, assign to this.state directly or define a state = {}; class property with the desired state in the MyComponent component. The " not yet mounted " part actually makes little to no sense as the only way to trigger the issue is to call a function by clicking a button from a component that needs to be

Correct way of Creating multiple stores with mobx and injecting it into to a Component - ReactJs

无人久伴 提交于 2019-12-18 02:57:28
问题 As suggested here in the Mobx documentation I have created multiple stores in the following manner: class bankAccountStore { constructor(rootStore){ this.rootStore = rootStore; } ... class authStore { constructor(rootStore){ this.rootStore = rootStore; } ... And finally creating a root store in the following manner. Also I prefer to construct children stores within master's store constructor. Moreover, I found that sometimes my child store has to observe some data from parent store, so I pass

The error of 'Duplicate string index signature' at Reactjs with Typescript

纵然是瞬间 提交于 2019-12-13 03:49:47
问题 I made an object using Typescript at Reactjs. There is a code below. But it makes this error in UserInformation data from password to lastname. Could you give me some advice? Duplicate string index signature import { observable, action } from 'mobx'; export interface ISignStore { email: string, password: string, firstName: string, lastName: string, handleInput(e: any): void, handleSubmit(e: any): void } export class SignStore implements ISignStore { @observable UserInformation: { [email:

Change route from store in React application

◇◆丶佛笑我妖孽 提交于 2019-12-12 09:45:48
问题 I have a React component that takes in username and password and sends it for authentication. If authentication is successful, the page should move to a different route which renders another component. Now the problem is, I am not sure how I can change route from my store? i.e. without using the <Link> component of React Router? I know that we can use this.props.history.push(/url) to change route programatically, but it can only be used inside a React Component using <Route> , not from the

Make single MobX autorun or reaction for observable property of all objects in array

冷暖自知 提交于 2019-12-12 03:48:23
问题 I have class with @observable (all examples are Typescript/pseudocode) class Page { id: number = 0; @observable isVisible: boolean = false; } let array = [new Page(), new Page(), new Page()]; And some functions like: changeVisibility(obj) { //ajax call like .post("/api/changeVisibility/", {id:obj.id, isVisible:obj.isVisible}) } And I want to react on isVisible change on any object. I can enumerate array and make something like: array.forEach(el => { reaction( () => el.isVisible, isVis =>