Git Product home page Git Product logo

Comments (7)

jrunning avatar jrunning commented on May 13, 2024

I'm seeing the same thing on 0.5.0. Here's a minimal repro:

import { h, render, Component, } from 'ink';

class Foo extends Component {
  componentDidMount () {
    setTimeout(() => this.setState({ a: 1 }));
  }

  render () {
    console.log('hihi')
    return 'X';
  }
}

render(<Foo/>);

The output is:

hihi
hihi
hihi
...many lines...
hihi
hihi
console.js:1
(function (exports, require, module, internalBinding) {// Copyright Joyent, Inc. and other Node contributors.
^

RangeError: Maximum call stack size exceeded
    at Console.log (console.js:1:1)
    at Console.console.(anonymous function).args [as log] (/path/to/repo/node_modules/ink/index.js:80:15)
    at Foo.render (/path/to/repo/lib/test/test.js:11:13)
    at Foo._render (/path/to/repo/node_modules/ink/lib/component.js:54:15)
    at rerender (/path/to/repo/node_modules/ink/lib/diff.js:74:35)
    at diff (/path/to/repo/node_modules/ink/lib/diff.js:154:3)
    at build (/path/to/repo/node_modules/ink/lib/renderer.js:10:9)
    at Renderer.update (/path/to/repo/node_modules/ink/lib/renderer.js:25:20)
    at Console.console.(anonymous function).args [as log] (/path/to/repo/node_modules/ink/index.js:81:16)
    at Foo.render (/path/to/repo/lib/test/test.js:11:13)

If it's helpful, the here's the transpiled source:

'use strict';

var _ink = require('ink');

class Foo extends _ink.Component {
  componentDidMount() {
    setTimeout(() => this.setState({ a: 1 }));
  }

  render() {
    console.log('hihi');
    return 'X';
  }
}

(0, _ink.render)((0, _ink.h)(Foo, null));

from ink.

jrunning avatar jrunning commented on May 13, 2024

Oddly, if you change the last two lines to this:

const Bar = () => <Foo/>;
render(<Bar/>);

The error output changes to this:

hihi
hihi
/path/to/repo/node_modules/ink/lib/diff.js:21
                return vnode.instance._pendingState || vnode.instance.state;
                                      ^

TypeError: Cannot read property '_pendingState' of null
    at getNextState (/path/to/repo/node_modules/ink/lib/diff.js:21:25)
    at diff (/path/to/repo/node_modules/ink/lib/diff.js:146:87)
    at diff (/path/to/repo/node_modules/ink/lib/diff.js:163:21)
    at build (/path/to/repo/node_modules/ink/lib/renderer.js:10:9)
    at Renderer.update (/path/to/repo/node_modules/ink/lib/renderer.js:25:20)
    at Console.console.(anonymous function).args [as log] (/path/to/repo/node_modules/ink/index.js:81:16)
    at Foo.render (/path/to/repo/lib/test/test2.js:11:13)
    at Foo._render (/path/to/repo/node_modules/ink/lib/component.js:54:15)
    at rerender (/path/to/repo/node_modules/ink/lib/diff.js:74:35)
    at diff (/path/to/repo/node_modules/ink/lib/diff.js:154:3)

Those last two lines transpile to this:

const Bar = () => (0, _ink.h)(Foo, null);
(0, _ink.render)((0, _ink.h)(Bar, null));

from ink.

vadimdemedes avatar vadimdemedes commented on May 13, 2024

Thanks for reporting, I reproduced the error, will look into it.

from ink.

vadimdemedes avatar vadimdemedes commented on May 13, 2024

Found the problem, since Ink has a special handling code for console methods (it clears the output, logs output, then rerenders again), it causes an infinite render loop. I will see if Ink could log a warning to prevent doing this or suppress console.log() while render is in progress. To fix this for now, move console.log() out of render() ;)

from ink.

egoarka avatar egoarka commented on May 13, 2024

@vadimdemedes I would suggest composing console.log output with render one

class MyComponent
...
setState(
  { name: 'perform 2' }, 
  () => console.log('perform 3')
)
...

render() {
  console.log('perform 1')
  return <div>{this.state.name}</div>
}

output:

perform 1
perform 2
perform 3

from ink.

vadimdemedes avatar vadimdemedes commented on May 13, 2024

@egoarka I wouldn't want to do that, since it's not an expected behavior of component's render() function and it's not that simple.

from ink.

vadimdemedes avatar vadimdemedes commented on May 13, 2024

Shouldn't be a problem anymore in Ink 2, since there's no more patching of console.log().

from ink.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.