Passing params with router.back()
Asked Answered
E

4

10

I'm using expo-router and need to pass some params back. How to do that?

I use router push here:

<TouchableOpacity style={styles.qrButton} onPress={()=> {router.push(`/rent-bike/qr-scanner`)}}>
                    <Text style={styles.qrText}>Użyj kodu QR</Text>
                </TouchableOpacity>

Then I scan QR code and want to pass it back to display on same page:

    const handleBarCodeScanned = ({type, data}) => {
        setScanData(true)
        console.log(`data: ${data}`);
        console.log(`type: ${type}`);
        router.back()
    };

I can't see any solutions.

Espinosa answered 5/5, 2023 at 15:49 Comment(1)
Does this answer your question? How to pass data from a modal with Expo Router using React-NativeEmbrangle
I
1

How about using router.replace instead of goBack(). That way you can add params: router.replace({pathname: 'some-path', params: {param: qrData }}).

Invite answered 11/5, 2023 at 7:18 Comment(1)
This sort of works, but the visual effect has the screen moving in from the right instead of from the leftGoad
B
0

Go back then update the route parameters.

router.back()
router.setParams({ foo: "bar" })
Brandy answered 19/4, 2024 at 12:40 Comment(1)
This doesn't workCutthroat
A
0

I think we can pass params by specifying the screen name we want to return to in the pathname using router.navigate.

router.navigate({ pathname: "backToPath", params: { a: "paramA" }, });
Aphotic answered 15/6, 2024 at 7:4 Comment(0)
F
0

It is not (yet?) possible to add params using a router.back(). You have to use router.navigate() or another expo router functions.

Workaround (not the best, I admit)

I want to setParams when going back to my page in order to tell it to refetch the data. I can use useFocusEffect(() => {}); from expo-router in order to refetch the data every time we quit the page and then return to it (go back to focus on it).

(I admit it is not very efficient.)

Further answered 3/9, 2024 at 7:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.