Giter Club home page Giter Club logo

Comments (5)

mbrandonw avatar mbrandonw commented on May 23, 2024 4

Hey @alexito4 thanks for the report!

You are correct there is some weird behavior here, and it's something we've run into ourselves. We actually have this called out in the documentation:

/// Due to a bug in SwiftUI, there are times that use of this view can interfere with some core
/// views provided by SwiftUI. The known problematic views are:
///
/// * If a `GeometryReader` is used inside a `WithViewStore` it will not receive state updates
/// correctly. To work around you either need to reorder the views so that `GeometryReader`
/// wraps the `WithViewStore`, or, if that is not possible, then you must hold onto an explicit
/// `@ObservedObject var viewStore: ViewStore<State, Action>` in your view in lieu of using
/// this helper (see [here](https://gist.github.com/mbrandonw/cc5da3d487bcf7c4f21c27019a440d18)).
/// * If you create a `Stepper` via the `Stepper.init(onIncrement:onDecrement:label:)` initializer
/// inside a `WithViewStore` it will behave erratically. To work around you should use the
/// initializer that takes a binding (see
/// [here](https://gist.github.com/mbrandonw/dee2ceac2c316a1619cfdf1dc7945f66)).

This bug is even reproducible in vanilla SwiftUI code, so it's not related to TCA at all. We've filed feedbacks and have gotten a response that it is definitely a bug in SwiftUI.

You have two options for working around this. First, sometimes it's possible to put the GeometryReader on the outside of the WithViewStore, and then everything works correctly. We are able to do this in the animations case study:

GeometryReader { proxy in
WithViewStore(self.store) { viewStore in

It's not always possible to do that, but if it is then it's the easiest workaround.

If you can't flip the views then gotta use the ViewStore as a @ObservableObject, which requires a small amount of manual work. We demonstrate this in the voice memos app:

struct VoiceMemoView: View {
// NB: We are using an explicit `ObservedObject` for the view store here instead of
// `WithViewStore` due to a SwiftUI bug where `GeometryReader`s inside `WithViewStore` will
// not properly update.
//
// Feedback filed: https://gist.github.com/mbrandonw/cc5da3d487bcf7c4f21c27019a440d18
@ObservedObject var viewStore: ViewStore<VoiceMemo, VoiceMemoAction>

Can you give that a shot and let us know if it works out?

Hopefully this is fixed in SwiftUI soon!

from swift-composable-architecture.

mbrandonw avatar mbrandonw commented on May 23, 2024 1

@mbrandonw can you share a code snippet that reproduces the bug?

We've got links to feedbacks we have submitted to Apple in the documentation of WithViewStore:

/// * If a `GeometryReader` or `ScrollViewReader` is used inside a ``WithViewStore`` it will not
/// receive state updates correctly. To work around you either need to reorder the views so that
/// the `GeometryReader` or `ScrollViewReader` wraps ``WithViewStore``, or, if that is not
/// possible, then you must hold onto an explicit
/// `@ObservedObject var viewStore: ViewStore<State, Action>` in your view in lieu of using this
/// helper (see [here](https://gist.github.com/mbrandonw/cc5da3d487bcf7c4f21c27019a440d18)).
/// * If you create a `Stepper` via the `Stepper.init(onIncrement:onDecrement:label:)` initializer
/// inside a ``WithViewStore`` it will behave erratically. To work around you should use the
/// initializer that takes a binding (see
/// [here](https://gist.github.com/mbrandonw/dee2ceac2c316a1619cfdf1dc7945f66)).

Here's a gist of the feedback, which shows how to reproduce the bug in vanilla SwiftUI:

https://gist.github.com/mbrandonw/cc5da3d487bcf7c4f21c27019a440d18

from swift-composable-architecture.

alexito4 avatar alexito4 commented on May 23, 2024

We actually have this called out in the documentation

Dang! I'm so sorry! I totally read that days ago and I knew there was some issue with the geometry reader, in my mind that's why I nested it inside because otherwise the sizes were wrong. Now I realise that causes this other issue, sorry for not reading it again ^^'

I juggled some geometry readers around but ended up moving the viewStore to a property, seems safer and easier to maintain.

Thank you @mbrandonw 🤗

from swift-composable-architecture.

ferologics avatar ferologics commented on May 23, 2024

This is still an issue, if anyone's wondering.

from swift-composable-architecture.

doesnotexist avatar doesnotexist commented on May 23, 2024

This bug is even reproducible in vanilla SwiftUI code,

@mbrandonw can you share a code snippet that reproduces the bug?

from swift-composable-architecture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.