So I am trying to learn redux by following this video: https://www.youtube.com/watch?v=bvn_HYpix6s&list=PLGNi8CcDr1dLmW4ZzSMvf4gqfobXhSPlX&index=3&t=7181s
Trying to implement a map and created my data layer with setOrigin, setDestination and setTravelTimeInformation functions inside the reducer.
Here is the navSlice.js file:
import {createSlice} from "@reduxjs/toolkit";
const initialState = {
origin: 0,
destination: 0,
travelTimeInformation: 0,
};
export const navSlice = createSlice({
name: "nav",
initialState,
reducer: {
setOrigin: ( state, action ) => {
state.origin = action.payload;
},
setDestination: (state, action) => {
state.destination = action.payload;
},
setTravelTimeInformation: (state, action) => {
state.travelTimeInformation = action.payload;
},
},
});
export const {setOrigin, setDestination, setTravelTimeInformation}=
navSlice.actions;
// selectors
export const selectOrigin = (state) => state.nav.origin;
export const selectDestination = (state) => state.nav.destination;
export const selectTravelTimeInformation = (state) =>
state.nav.travelTimeInformation;
export default navSlice.reducer; // connects us to the store
I use my selector in Map.js file to show us the location that user entered in app:
import React from 'react'
import { StyleSheet, Text, View } from 'react-native'
import MapView , { Marker } from "react-native-maps";
import tw from "tailwind-react-native-classnames";
import { useSelector } from 'react-redux';
import { selectOrigin } from '../slices/navSlice';
const Map = () => {
// this hook takes a selector function as an argument
const origin = useSelector(selectOrigin); // it is an object (returns null and it causes to error rn )
return (
<MapView
style = {tw`flex-1`}
mapType = "mutedStandart"
initialRegion={{ // info about the location we chose
latitude: origin.location.lat,
longitude: origin.location.lng,
latitudeDelta: 0.005,
longitudeDelta: 0.005,
}}
/>
)
}
export default Map
const styles = StyleSheet.create({})
and here the HomeScreen.js which includes the to let us use setOrigin with the given input.
import React from 'react'
import { StyleSheet, Text, View, SafeAreaView, Image} from 'react-native'
import tw from "tailwind-react-native-classnames";
import NavOptions from '../Components/NavOptions';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
import {GOOGLE_MAPS_APIKEY} from "@env";
import { useDispatch } from 'react-redux';
import {setDestination, setOrigin} from "../slices/navSlice";
const HomeScreen = () => {
const dispatch = useDispatch();
return (
<SafeAreaView style = {tw`bg-white h-full`}>
<View style = {tw`p-10`}>
<Image
style = {{
width: 100, height: 100, resizeMode: "contain",
}}
source = {{
uri: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACoCAMAAABt9SM9AAAAgVBMVEX///8AAACkpKT09PTi4uKgoKCurq7a2tqJiYn4+PjIyMjx8fHq6uqEhIRycnKnp6fOzs7BwcHU1NSQkJBGRkZUVFS4uLhnZ2d6enpsbGy7u7uWlpZzc3NAQECsrKwtLS0aGhosLCwiIiJKSkpcXFwSEhI3NzcXFxdSUlIzNDQLCwva1CnEAAAI40lEQVR4nO2caVvqPBCGadksFFlUBFQUxIXz/3/gi/QAySzJTAvF815zf2zTafI0yySZtNEwDMMwDMMwDMMwDMMwDMMwDMMwDMMwDMMwDMMwLkTXR/18xhvIK5r+fSQ+Te3zLWDAudX272RnzPWVuKBYqYkFMLEUmFgKTCwFJpYCE0uBiaXAxFJgYikwsRSYWApMLAUmlgITS4GJpcDEUmBiKTCxFJhYCkwsBSaWAhNLgYmlwMRSUE2srNWbLtN02BlP8vJlACYnvR2TVv9MBh2uJ1Y3ffzjJdjOptUUy6ezF9fg6+2wYtBA1h13humO9j73VxKrO0hI3u5aJcs1GfwhLc7HsSf99J1THm9WQJiriDV9paXas+2gl0TJ2h8Bi4NwhaXFWqy8y9cSqxMoV0Fbl4eMqaYnNqH6SonV+qSEqV2s3ltUq8RtDHGeJAbnfMmIN6fo+WuIlT1KSrZjJe2ae0KDyVAu1i1++gpiiUu24070fqJcHPeMp+en2olFfdD6xbqRl2zHZ9xTyr9UFicisWbUo7WL9aAqGVu4E5qKuofsCv0k0wX5ZM1iNbfaoiXJIvju+LCKoMZZPwUzXNQs1kpftPCoOCxjkFBL9FzNYpVjyr55Ws4gll/02C8QazW/Szud4dPsm03SY148opOvn9vT3mg07txtGIOjf1KsgZft1vCeTkY73zmV9L7jDaDdO77cWrH61xTrnagxOela/CHfS8wDHglZF2uc7kUq1tvNu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BF1jWMZ9RP7VaD2DKqV4Jl6Z21UTpzHNXPebt3zr9J54ubzfq81pu9dPZwWObZbgbDUd2/v87yHeoAebH1/iWtG4ZhGIZhGIZhGIZhGIZhGIZhGIZhGIZhGIZhGL+e/wCc4mrZXZ1WHAAAAABJRU5ErkJggg=="
}}
/>
<GooglePlacesAutocomplete
placeholder = "Where from?"
styles = {{
container: {
flex: 0,
},
textInput: {
fontSize: 18,
},
}}
onPress = {(data, details = null) => {
// details include infos like coordinates etc.
dispatch(setOrigin({
location: details.geometry.location,
description: data.description
}))
dispatch(setDestination(null));
// console.log(details);
} }
fetchDetails = {true}
returnKeyType = {"search"}
enablePoweredByContainer = {false}
minLength = {2}
query = {{
key: GOOGLE_MAPS_APIKEY,
language: "en",
}}
nearbyPlacesAPI = "GooglePlacesSearch"
debounce = {400}
/>
<NavOptions/>
</View>
</SafeAreaView>
)
}
export default HomeScreen
const styles = StyleSheet.create({})
and here is the store.js file:
import { configureStore } from '@reduxjs/toolkit';
import navReducer from "./slices/navSlice";
export default configureStore({
reducer: {
nav: navReducer,
},
})
So the first error is "setOrigin is not a function" ( which i used in dispatch() ) and the other error is occurs in when I'm doing assignments to latitude and longitude. I think the reason of this is same as the first one,
const origin = useSelector(selectOrigin);
This does not return a object so origin is null i guess.
I tried both setting initialState to 0 and null. But it did not fixed bug.
Why does it happen?
Terminal output:
(0, _navSlice.setOrigin) is not a function. (In '(0, _navSlice.setOrigin)({
location: details.geometry.location,
description: data.description
})', '(0, _navSlice.setOrigin)' is undefined)
OK, I've solved the problem: The "reducer:" must be "reducers:" in the navSlice.js