Crank: Skip rendering after prop update

Created on 1 May 2020  路  5Comments  路  Source: bikeshaving/crank

How can you skip an update after a prop update as you need to yield on each iteration?

Most helpful comment

If you have a stateful component you can render a special <Copy /> element instead of what you rendered previously to prevent the children from updating.

function equals(props, newProps) {
  for (const name in {...props, ...newProps}) {
    if (props[name] !== newProps[name]) {
      return false;
    }
  }

  return true;
}

function memo(Component) {
  return function *Wrapped({props}) {
    yield <Component {...props} />;
    for (const newProps of this) {
      if (equals(props, newProps)) {
        yield <Copy />;
      } else {
        yield <Component {...newProps} />;
      }

      props = newProps;
    }
  };
}

All 5 comments

If you have a stateful component you can render a special <Copy /> element instead of what you rendered previously to prevent the children from updating.

function equals(props, newProps) {
  for (const name in {...props, ...newProps}) {
    if (props[name] !== newProps[name]) {
      return false;
    }
  }

  return true;
}

function memo(Component) {
  return function *Wrapped({props}) {
    yield <Component {...props} />;
    for (const newProps of this) {
      if (equals(props, newProps)) {
        yield <Copy />;
      } else {
        yield <Component {...newProps} />;
      }

      props = newProps;
    }
  };
}

You can read more about the special element tags here: https://crank.js.org/guides/special-tags-and-props

That鈥檚 beautiful

Wouldn't it be possible to implement this feature with yield null?

@lazeebee I鈥檓 pretty sure that yield nullwill make your component render nothing which is what most would expect to happen. Copy will keep the UI the same without actually calling render again

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcjazzyfunky picture mcjazzyfunky  路  5Comments

lukejagodzinski picture lukejagodzinski  路  4Comments

virtualfunction picture virtualfunction  路  5Comments

dfabulich picture dfabulich  路  4Comments

GormanFletcher picture GormanFletcher  路  4Comments