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

Force a node to be replaced, and not patched #1189

Closed
juanmiguelbesada opened this Issue Oct 30, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@juanmiguelbesada
Contributor

juanmiguelbesada commented Oct 30, 2018

Stencil version:

 @stencil/core@0.15.1

I'm submitting a:

[] bug report
[x] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior:

There is no way to force stencil to replace a child node. At this moment, when you update a state, it triggers render, but it patch the vnodes but not replace is. This cause that if one vnode has some type of initialization (in my use case is a amp-img element) it do not trigger the initialization again when update.

Expected behavior:
Allow somehow to force the vnodes to be replaced instead of patched. Or just replace the whole component

Steps to reproduce:

Related code:

//TodoList
import { Component, State, Method } from '@stencil/core';
import { Todo } from '../../declarations/Todo';

@Component({
  tag: 'todo-list'
})
export class TodoList {
  cont: number = 2;
  @State() todos: Todo[] = [
    {id: "1", value: 1},
    {id: "2", value: 2},
  ];

  @Method()
  addTodo() {
    this.cont++;
    this.todos = [...this.todos, {id: this.cont.toString(), value: this.cont}];
  }

  @Method()
  editTodo() {
    this.todos[1] = {...this.todos[1], value: this.todos[1].value + 1};
  }

  render() {
    return (
      <div class="todo-list">
        {this.todos.map(todo => (<todo-item todo={todo}></todo-item>))}
        <button type="button" onClick={() => this.addTodo()}>Add todo</button>
        <button type="button" onClick={() => this.editTodo()}>Edit todo</button>
      </div>
    );
  }
}
//TodoItem
import { Component, Prop, ComponentDidLoad, ComponentWillUpdate, ComponentDidUpdate, ComponentWillLoad, Watch } from '@stencil/core';
import { Todo } from '../../declarations/Todo';

@Component({
  tag: 'todo-item'
})
export class TodoItem implements ComponentWillLoad, ComponentDidLoad, ComponentWillUpdate, ComponentDidUpdate {
    @Prop() todo: Todo;

    fooEl!: HTMLElement;

    componentWillLoad() {
        console.log('componentWillLoad');
    }

    componentDidLoad() {
        console.log('componentDidLoad');
        //Do some component initialization
        this.fooEl.style.borderWidth = this.todo.value + 'px';
    }

    componentWillUpdate() {
        console.log('componentWillUpdate');
    }

    componentDidUpdate()
    {
        console.log('componentDidUpdate');
    }

    render() {
        return (
            <div class="todo-item">
                <div style={{border: "1px solid red"}} ref={(el) => this.fooEl = el}>{this.todo.id} - {this.todo.value}</div>
            </div>
        );
    }
}

Other information:
At TodoItem.componentDidLoad we just set the borderWith to the todoValue. (This is just to simulate what amp-img do). We have no way to replace the fooEl. If i remove the note in the componentWillUpdate event, the render will not create a new node.

@ionitron-bot ionitron-bot bot added the triage label Oct 30, 2018

@adamdbradley

This comment has been minimized.

Member

adamdbradley commented Oct 31, 2018

Thank you for your feedback, but recreating the same node within the patch function is not a feature we'll be adding to Stencil. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment