Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested custom component loses scope #44

Open
titoBouzout opened this issue Dec 13, 2021 · 0 comments
Open

Nested custom component loses scope #44

titoBouzout opened this issue Dec 13, 2021 · 0 comments

Comments

@titoBouzout
Copy link
Contributor

On the following example, the second click function loses the scope and doesn't work. I presume is realted to this issue #31

live example https://codesandbox.io/s/mobx-nested-custom-component-with-spread-pfxo4?file=/index.js

import { render, Component, spread } from "mobx-jsx";

function Box(props) {
  let el = document.createElement("div");
  spread(el, () => props);
  return el;
}

class App extends Component {
  render() {
    return (
      <div>
        <div>
          <Box onClick={() => this.didClick()}>click me (this works)</Box>
        </div>
        <Box>
          <Box onClick={() => this.didClick()}>click me (this doesnt work)</Box>
        </Box>
      </div>
    );
  }
  didClick() {
    console.log("did click");
  }
}

render(() => <App />, document.getElementById("app"));

gets compiled as

  class App extends Component {
    render() {
      return (() => {
        const _el$ = _tmpl$.cloneNode(true),
              _el$2 = _el$.firstChild;

        insert(_el$2, createComponent(Box, {
          onClick: () => this.didClick(),
          children: "click me (this works)"
        }));

        insert(_el$, createComponent(Box, {
          get children() {
            return createComponent(Box, {
              onClick: () => this.didClick(),
              children: "click me (this doesnt work)"
            });
          }

        }), null);

        return _el$;
      })();
    }

    didClick() {
      console.log('did click');
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant