omniscientjs / immstruct

Immutable data structures with history for top-to-bottom properties in component based libraries like React. Based on Immutable.js
374 stars 21 forks source link

Remove current cursor. #83

Closed DylanPiercey closed 9 years ago

DylanPiercey commented 9 years ago

Lets say you have a "TodoItem" which is rendered inside of a "TodoList".

The todolist might pass each item a cursor:

<TodoItem todo={ struct.cursor(i) }/>

Now, without giving the todoitem access to the entire todolist it would be handy if the "TodoItem" could remove itself like so:

// Inside TodoItem render function.
let { todo } = props;
<button onClick={ (e)=> todo.remove() }/>

This would be pretty handy as currently I'd have to pass down a "requestRemove/onRemove" handler from the parent.


Edit: This is probably a limitation of immutable.js.

mikaelbr commented 9 years ago

Yes, I'm afraid that's a limitation/design choice from Immutable.js' side. What you'd typically do, is instead design an "API" where you pass down your actions to your elements:

var TodoItem = component(function ({onClick, cursor}) {
  return (<div>
    <button onClick={onClick.bind(null, cursor)}/>
    <strong>{cursor.deref()}</strong>
  </div>);
});

// and from parent:
var TodoList = component(function ({listCursor}) {
  var remove = function (itemCursor) {
    return listCursor.remove(itemCursor);
  };
  return (<div>
    <TodoItem onClick={remove} cursor={listCursor.cursor('0')} />
  </div>);
});