question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't work with decorator

See original GitHub issue

I have the following code:

import React, { Component, PropTypes } from 'react';
import LinkedStateMixin from 'react-addons-linked-state-mixin';
import ReactMixin from 'react-mixin';
import s from './LoginPage.scss';
import withStyles from '../../decorators/withStyles';

const title = 'Log In';

@withStyles(s)
class LoginPage extends Component {

  constructor() {
    super();
    this.state = {
      user: '',
      password: ''
    }
  }

  static contextTypes = {
    onSetTitle: PropTypes.func.isRequired,
  };

  componentWillMount() {
    this.context.onSetTitle(title);
  }

  login(e) {
    e.preventDefault();
    console.log(e)
  }

  render() {
    return (
      <div className={s.root}>
        <div className={s.container}>
          <h1>{title}</h1>
          <form role="role">
            <div>
              <label>
                Email
              </label>
              <input type="email" valueLink={this.linkState('email')} placeholder="email" />
            </div>
            <div>
              <label>
                Password
              </label>
              <input type="password" valueLink={this.linkState('password')} placeholder="password" />
            </div>
            <button type="submit" onClick={this.login.bind(this)}>Login</button>
            <p>
              Forgot your password?
            </p>
          </form>
        </div>
      </div>
    );
  }

}

ReactMixin(LoginPage.prototype, LinkedStateMixin);

export default LoginPage

The react-mixin functionality works as intended without a decorator, however, when a decorator is included I get the error "this.linkState is not a function’

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
brigandcommented, Jan 20, 2016

So the lack of autobinding in react-mixin strikes again… it’s in the readme that it’s not supported, but maybe it’s time to support it.

0reactions
Lonelindcommented, Jan 20, 2016

Sure.

For example, we could get a Lifecycle mixin form react-router v1.x (in 2.x mixins are deprecated). That’s what I faced:

import React from 'react';
import { decorate as mixin } from 'react-mixin';
import { Lifecycle } from 'react-router';

@mixin(Lifecycle)
export default class RouteComponent extends React.Component {
    routerWillLeave(nextRoute) {
        console.log(this.state, this); // 'this' will be an anonymous function
    }

    render() {
        /* ... */
    }
}

routerWillLeave method is provided from mixin. If you use React.createClass, all methods are bond to this class by default. And you can use it in render just like <div onClick={this.handleClick} />. But if you use ES6 classes, NOTHING will be bond and you should bind every method by yourself. The same happens when mixin adds routerWillLeave. Everything should be bond to class explicitly. So, maybe this is responsibility of mixin maker, and if so, your solution won’t work if there weren’t any explicit binding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python decorator doesn't work - Stack Overflow
I am new to python. I want to pass the arguments to a class derorator: class TestClass(object): def __init__( ...
Read more >
Parameter decorator doesn't work when applying to symbol ...
I find that I cannot use parameter decorators on symbol methods. In ts-node it results in target[key] being undefined , while on the...
Read more >
Code Completion doesn't work for (class) functions decorated ...
Code Completion doesn't work for (class) functions decorated with decorators that return inner functions Follow.
Read more >
Please Fix Your Decorators - Hynek Schlawack
If your Python decorator unintentionally changes the signatures of my callables or doesn't work with class methods, it's broken and should ...
Read more >
Primer on Python Decorators
The problem is that the inner function wrapper_do_twice() does not take any arguments, but name="World" was passed to it. You could fix this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found