# Is \`Environment\` being initialized for every action fired intentional?

**URL:** <https://forums.swift.org/t/is-environment-being-initialized-for-every-action-fired-intentional/39779>\
**Category:** Swift Composable Architecture\
**Created:** [August 27, 2020, 7:45pm UTC](https://forums.swift.org/t/is-environment-being-initialized-for-every-action-fired-intentional/39779 "2020-08-27T19:45:01Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![peterka09](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peterka09/32/20084_2.png) [@peterka09](https://forums.swift.org/u/peterka09)\
**Post date:** [August 31, 2020, 10:18pm UTC](https://forums.swift.org/t/is-environment-being-initialized-for-every-action-fired-intentional/39779/3 "2020-08-31T22:18:53Z")

</div>

> [@joshhaines](#):
>
> You mention you are initializing your environment in a closure? Can you provide an example?

We are running into this issue when we call `store.scope` and create a view, `FooDetailState`, from `FooState`. A small example would be

```swift
// MARK: - State
struct FooState {
    var detail: FooDetail
}
struct FooDetail { ... }

// MARK: - Environment
struct FooEnvironment {
    let client: PathMonitorClient
}
struct FooDetailEnvironment {
    let client: PathMonitorClient
}

```

The closure we're referring to is provided in the pullback of `fooDetailReducer`

```swift
let fooReducer: Reducer<FooState, FooAction, FooEnvironment> = Reducer.combine(
    fooDetailReducer
    .pullback(
        state: \.detail,
        action: ...
        environment: { env in 
            .init(client: env.client) 
        }
    ),
    ...
)

```

Then in the view we'd have something like

```swift
/// FooView
var body: some View {
...
    FooDetailView(store: store.scope(state: \.detail, action: FooAction.detail)
...
}

```

What we're noticing is that each time an action is sent to the Store and processed in the reducer, the breakpoint set on the closure `environment: { env in .init(client: env.client) }` is getting hit--leading us to believe a new instance of `FooDetailEnvironment` is being created.

---

_[View the full topic](https://forums.swift.org/t/is-environment-being-initialized-for-every-action-fired-intentional/39779)._
