jesuisgenial
jesuisgenial

Reputation: 725

React native + Mobx, @inject decorator throws an error

I'm trying to use mobx with react native and stuck into a problem.

@inject('someStore')
@observer
export class SomeComponent extends Component {
   render() {
       ...
   }
}

I'm sure I configured properly babel plugins for decorator but @inject decorator gives me an exception "Expected a constructor.".

enter image description here

I have no idea why this happen since I had used mobx in this way in an other project. Does anyone had been through this issue?

Upvotes: 6

Views: 1446

Answers (3)

mkilincaslan
mkilincaslan

Reputation: 39

"mobx-react": "^6.1.3" it works for me

import React, { Component } from 'react';
import {observer, inject} from 'mobx-react';

class SomeComponent extends Component {
class_content
}

export default inject('someStore', 'someStore' /* here you can add as many store files as you need */)(observer(SomeComponent));

But don't forget to add Provider to your App.js or which is your main file

Upvotes: 1

SkyTreasure
SkyTreasure

Reputation: 884

Rewriting the class as below worked for me

"mobx": "^5.13.0","mobx-react": "^6.1.3"

class LoginScreen extends React.Component {

}

export default inject("userStore")(observer(LoginScreen));

Upvotes: 1

bkm412
bkm412

Reputation: 1057

I downgraded the mobx-react version to 5.4.4 and I can confirm that it works.

Try to downgrade mobx-react.

yarn add [email protected]

Upvotes: 3

Related Questions