0

I'm a JS/React/MobX/else newbie, trying to do something very simple without success.

What I want is - 2 buttons, once clicked, show corresponding tab in a modal, using react-bootstrap and mobx.

But I keep getting this error:

[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: 'Reaction[AuthModal#0.render()] Error: [mobx] Invariant failed: Side effects like changing state are not allowed at this point. Are you trying to modify state from, for example, the render function of a React component? Tried to modify: AuthModalStore@3.tab

Please help. Much appreciated.

code for the store:

import { observable } from 'mobx';

class AuthModalStore {

  @observable show = false;
  @observable tab = 1;

  constructor() {
    this.turnOn = this.turnOn.bind(this);
    this.turnOff = this.turnOff.bind(this);
    this.changeToTab = this.changeToTab.bind(this);
  }

  turnOn(key = 1) {
    console.log('running turnOn');
    this.tab = key;
    this.show = true;
  }

  turnOff() {
    this.show = false;
  }

  changeToTab(key) {
    this.tab = key;
  }

}

const store = new AuthModalStore();

export default store;

react component:

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

import AuthModal from './AuthModal';


@inject('authModalStore')
@observer
export default class AuthButtons extends React.Component {

  handleClick(event) {
    var key;
    switch (event.target.innerHTML) {
      case 'Log In':
        key = 1;
        break;
      case 'Sign Up':
        key = 2;
        break;
      default:
        key = 1;
    }
    this.props.authModalStore.turnOn(key);
  }

  render() {
    return (
      <div className="nav navbar-nav navbar-right">
        {/* login / signup buttons in navbar */}
          <ul className="nav navbar-nav btn-toolbar">
            <li className="btn-group">
              <p className="navbar-btn text-uppercase">
                <a onClick={this.handleClick.bind(this)} className="btn btn-default">Log In</a>
              </p>
            </li>
            <li className="btn-group">
              <p className="navbar-btn text-uppercase">
                <a onClick={this.handleClick.bind(this)} className="btn btn-primary">Sign Up</a>
              </p>
            </li>
          </ul>
          <AuthModal />
        </div>
      );
    }
  }
Root
  • 1
  • 1
  • 2

3 Answers3

1

I see you're changing the mobx-state without the wrapped code being in an action function.

If a function changes the state, then it should be decorated as an action

https://mobx.js.org/refguide/action.html

Actions should only, and always, be used on functions that modify state. Functions that just perform look-ups, filters etc should not be marked as actions; to allow MobX to track their invocations.

Also, if the functions should be bound to the class, then you can use the decorator:

@action.bound

I hope I helped.

Eksapsy
  • 624
  • 2
  • 12
  • 25
0

answer was provided here: https://github.com/mobxjs/mobx/issues/829

use this:

<a onClick={() => this.props.authModalStore.changeToTab(2)}>Not a member yet?</a>
<a onClick={() => this.props.authModalStore.changeToTab(3)} className="pull-right">Forgot password?</a>

instead of

<a onClick={this.props.authModalStore.changeToTab(2)}>Not a member yet?</a>
<a onClick={this.props.authModalStore.changeToTab(3)} className="pull-right">Forgot password?</a>
Robin F.
  • 905
  • 9
  • 17
0

If you want to change something in your store you need to use action as mobx state here

Your import should look like this:

import { action, observable } from 'mobx';

Your function should look like this:

@action
turnOn(key = 1) {
  console.log('running turnOn');
  this.tab = key;
  this.show = true;
}

In Mobx you cannot mutate the observable outside the action.

Ibrahim
  • 186
  • 12