Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

Pass props from screen to withObservables #1767

Closed Nidilap closed 7 months ago

Nidilap commented 7 months ago

Hello.

I am trying to pass props from a screen to an withObservables used to enhance the screen. I am using react navigation, so the data I'm trying to pass comes from it.

This is my screen:

const TestScreen = (props: any) => {
    console.log("props: ", props);

    return (
        <View>
        </View>
    );
};

And this is how I'm trying to use withObservables:

const enhance = withObservables(['props'], (props) => ({
  idTest2: props.idTest.observe(),
}))
export default enhance(TestScreen);

Obs.: I'll not use like this, the idea is to use props.idTest as a query to database.get() to filter some models. I am just doing this to test if I can get the props inside the withObservables.

I saw this issue that seems to be the same problem as me: https://github.com/Nozbe/WatermelonDB/issues/1397 However, it is closed. Stophface used recompose to do what I am trying to do, but it is an archived library, so I don't want to use it.

Is there a way to do this within WatermelonDB or using another library that is still maintained?

Nidilap commented 7 months ago

I was doing it wrong. I managed to do it like this: const enhance = withObservables(["route"], ({ route }) => ({ batches: watermelonDB.get(TableName.BATCH).query(Q.where("id", route?.params?.idTest)).observe() }));

And in my component: const TestScreen = ({ route, batches }) => { [...]