DEV Community

Ola Abaza
Ola Abaza

Posted on

1 RN Thing a Day – Day 14: Predicate & Effect Patterns in React Native

The easiest way to think about it is:

Predicate = Watch for something

Effect = Do something when it happens

Example:

predicate: Is someone entering the building?
effect: Check their ID
Enter fullscreen mode Exit fullscreen mode

Example 1: Add Item To Cart

// cartSlice.ts

import { createSlice, PayloadAction } from '@reduxjs/toolkit'

interface Product {
  id: string
  name: string
}

interface CartState {
  items: Product[]
}

const initialState: CartState = {
  items: []
}

const cartSlice = createSlice({
  name: 'cart',
  initialState,
  reducers: {
    addItem: (state, action: PayloadAction<Product>) => {
      state.items.push(action.payload)
    },

    removeItem: (state, action: PayloadAction<string>) => {
      state.items = state.items.filter(
        item => item.id !== action.payload
      )
    }
  }
})

export const { addItem, removeItem } = cartSlice.actions

export default cartSlice.reducer

// listenerMiddleware.ts

import { createListenerMiddleware } from '@reduxjs/toolkit'
import Toast from 'react-native-toast-message'

export const listenerMiddleware =
  createListenerMiddleware()

// cartListeners.ts

import { listenerMiddleware } from './listenerMiddleware'

listenerMiddleware.startListening({
  predicate: (_, currentState, previousState) => {
    return (
      currentState.cart.items.length >
      previousState.cart.items.length
    )
  },

  effect: async () => {
    Toast.show({
      type: 'success',
      text1: 'Item added to cart'
    })
  }
})
Enter fullscreen mode Exit fullscreen mode

Why use a predicate instead of listening to addItem directly?
You could do:

listenerMiddleware.startListening({
  actionCreator: addItem,
  effect: () => {
    Toast.show({
      type: 'success',
      text1: 'Item added to cart'
    })
  }
})
Enter fullscreen mode Exit fullscreen mode

But the predicate version is more powerful because it reacts to state changes, not specific actions.

The predicate doesn't know or care which action happened. It only cares about the resulting state.

For example, if items are added by:

dispatch(addItem(product))
dispatch(syncCartFromServer())
dispatch(restoreSavedCart())
Enter fullscreen mode Exit fullscreen mode

the same predicate still works:

currentState.cart.items.length >
previousState.cart.items.length
Enter fullscreen mode Exit fullscreen mode

because it only cares that the cart grew, regardless of which action caused it. That's the main strength of the Predicate & Effect pattern.

Why Compare Current State and Previous State?
Because many actions happen in the app.

For example: User adds item to cart

Enter fullscreen mode Exit fullscreen mode

Without comparing states, your effect might run every time.

Instead, the predicate asks:

Did the specific thing I care about change?

Enter fullscreen mode Exit fullscreen mode

Only then does it execute the effect.

When should I use actionCreator?
Use it when your business rule is: "When this action happens, do something."

Examples:


Track Login Event
startListening({
  actionCreator: loginSuccess,
  effect: () => {
    Analytics.track('Login')
  }
})
Enter fullscreen mode Exit fullscreen mode

You specifically want to react to the login action.

When should I use predicate?
Use it when your business rule is:"When the state becomes this, do something."

Examples:

User Became Logged In
predicate: (_, current, previous) =>
  !previous.app.isLoggedIn &&
  current.app.isLoggedIn
Enter fullscreen mode Exit fullscreen mode

Maybe several actions can lead to a logged-in state:

loginSuccess()
restoreSession()
refreshTokenSuccess()
Enter fullscreen mode Exit fullscreen mode

You don't want to listen to all of them.

You just care that: isLoggedIn changed from false to true

Why not just use useEffect?

For small apps, useEffect is often enough. Predicate & Effect becomes valuable when you want to move business logic outside the UI layer.

The Problem With useEffect
Imagine this: When a user logs in, you need to:

Fetch profile
Load permissions
Connect websocket
Start analytics session

Many developers write:

function HomeScreen() {
  const isLoggedIn = useSelector(selectIsLoggedIn)

  useEffect(() => {
    if (isLoggedIn) {
      dispatch(fetchProfile())
      dispatch(loadPermissions())
      connectWebSocket()
      startAnalytics()
    }
  }, [isLoggedIn])
}
Enter fullscreen mode Exit fullscreen mode

Now ask yourself:

Why is HomeScreen responsible for login behavior?

It isn't really a UI concern.

It's application behavior.

The app should perform these actions whenever a user becomes logged in, regardless of which screen is mounted.

This is where Predicate & Effect shines.

Simple Rule

Use useEffect when:

The effect belongs to the component.

Examples:

  • Focus input
  • Start animation
  • Listen to keyboard
  • Set navigation title
  • Fetch data only for this screen

Use Predicate & Effect when:
The effect belongs to the application.

Examples:

  • User logged in
  • User logged out
  • Network restored
  • Language changed
  • Permissions updated
  • Token expired
  • Cart became non-empty
  • Analytics tracking
  • Background synchronization

Top comments (0)