facebookarchive / flux

Application Architecture for Building User Interfaces
https://facebookarchive.github.io/flux/
Other
17.42k stars 3.47k forks source link

Container create context issues after Babel 7.9.x update. #489

Closed pmysinski closed 2 years ago

pmysinski commented 4 years ago

After Babel 7.9.x Update exactly @babel/preset-env -> @babel/plugin-transform-classes

We have issue with proper context in Container components. Constructor has different context than render function. Until the update similar code to this one had been working properly (v7.8.6), we could bind functions in constructor and pass them to children components as props.

Example reproduction:

import React from 'react';
import FluxUtils from 'flux/utils';
import { Dispatcher } from 'flux';

class Store extends FluxUtils.ReduceStore {
  constructor() { super(new Dispatcher()); }
  getInitialState() { return {}; }
  reduce(state) { return state; }
}

const store = new Store();
let self;
class Component extends React.Component {

  static getStores() { return [store] }
  static calculateState() { }

  constructor() {
    super();
    this.foo = this.foo.bind(this);
    self = this;
  }

  foo() {
    console.log(this === self); 
  }

  render() {
    console.log(this === self); // false 
    this.foo() // false
    const obj = Object.assign({}, {foo: this.foo })
    obj.foo(); // foo is not binded so will be called with obj context so print false
    return (
      <p> hello</p>
    );
  }
}

export default FluxUtils.Container.create(Component);

Babel:

  presets: [
    [
      '@babel/preset-env',
      {
        useBuiltIns: 'entry',
        corejs: '3.6'
      }
    ]
  ]

package.json

...
    "@babel/preset-env": "7.9.0",
    "@babel/preset-react": "7.9.0",
...

I'm not sure whos issue this is babel or flux but it would nice to have this back to work how it used to:)

kyldvs commented 4 years ago

Thanks for the report! I've switched teams and haven't worked on Flux in quite a while, if anyone is able to help debug this and submit a PR that would be great. I will be able to merge it and do a release. If not this may be a bit slower to get resolved.

rvignacio commented 4 years ago

Had the same error, seems to be caused by the older Babel version used to generate the dist files for this repo. I updated my build to have current Babel transpile the source files and my app works again.

koba04 commented 4 years ago

@pmysinski @rvignacio I've updated Babel to the latest version and published it as @koba04/flux temporally. https://github.com/facebook/flux/pull/495 It would be helpful for me if you would test the pacakge whther it works fine or not.

rvignacio commented 3 years ago

@koba04 I confirm your fix works, thank you!

pmysinski commented 2 years ago

it works.