mobxjs / mobx-react-lite

Lightweight React bindings for MobX based on React 16.8 and Hooks
https://mobx.js.org/react-integration.html
MIT License
2.13k stars 90 forks source link

How to use modifier in mobx-react-lite #291

Closed geohuz closed 4 years ago

geohuz commented 4 years ago

Is it possible to use observable.deep/shallow in mobx-react-lite? I tried the following code but get errors:

const groupStore = useLocalStore(()=> ({
    rows: observable.shallow([]),
    addRow: (payload) => {
      let newRow = new groupRow()
      newRow.setRowStore(payload)
      groupStore.rows.push(newRow)
    }
  }))
kubk commented 4 years ago

It seems like it's not possible with useLocalStore. You can also use useState to create a local store:

class GroupStore {
  @observable.shallow rows = [];

  addRow(payload) {
    let newRow = new groupRow()
    newRow.setRowStore(payload)
    groupStore.rows.push(newRow)
  }
}

...

const [groupStore] = useState(() => new GroupStore());
danielkcz commented 4 years ago

If you have further questions, please open a new issue.