Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the logic go with SwiftUI compared to React?

The SwiftUI documentation is challenging to find out what is actually happening on each render / when they occur / what is causing them (or I don't know where to look!)

I'm trying to avoid building a separate class to do logic and referencing it from my views, I'm just not understanding where logic goes and the official tutorial doesn't go into it much. I'm attempting to build a component based system like React where children are independent and don't have to rely on a helper class.

To be clear, I think the answer is 1. below.


Here is what I don't get.

In react I can say:

  const [someStateVariable, setSomeStateVariable] = useState("")
  const dependsOnStateVariable = someStateVariable ? 0 : 1;

But in SwiftUI I cannot do:

@State var someStateVariable: String = ""
var dependsOnSomeStateVariable = someStateVariable ? 0 : 1

The same goes for doing logic with props being passed in (initialization variables). In react, I can do things with those props. In Swift, I cannot.

  1. Does all this logic have to go into init? I guess from reading struct documentation that makes sense, I just want to make sure I understand it correctly.

  2. Where is the documentation on the diffing algorithm and lifecycle events? What triggers a render? When is init run? On each render (ie. the properties passed in change or the state changes) or the first time it appears (I'm guessing on each render since onAppear exists).

  3. How do I respond to side effects? It appears it's a bit complex. Is there a useEffect equivalent? Code I can run only when certain state or input variables are changed?

like image 218
Diesel Avatar asked Jan 22 '20 06:01

Diesel


People also ask

Is SwiftUI similar to React?

Once you get over the syntax differences, SwiftUI feels a lot like React: It allows developers to create declarative user interfaces, meaning the UI that is rendered is a direct result of app state. This is important because the views that are rendered remain in sync with the data that drives them.

Is SwiftUI better than react native?

JavaScript is already mastered by thousands of developers which makes React Native a preferable choice for cross-platform development. Swift, on the other hand, allows developers to create native iOS applications which can be pricier but provide a better user experience.

Should I learn SwiftUI or react native?

The main difference between React Native and Swift is that React Native is best for cross-platform development, while Swift is for building iOS apps. Thus, you may develop an Android application in Java or Kotlin, for example, and choose Swift and Objective-C for iOS only.

Why use Reactjs?

One of the main benefits of using React JS is its potential to reuse components. It saves time for developers as they don't have to write various codes for the same features. Furthermore, if any changes are made in any particular part, it will not affect other parts of the application.


1 Answers

You can make dependsOnSomeStateVariable a computed property.

var dependsOnSomeStateVariable: Int {
    someStateVariable ? 0 : 1
}

As far as I know the exact View lifecycle is not documented (but some of it has been reverse engineered

For observed objects (@ObservedObject) you can listen for published changes (objectWillChange, usually in combination with DispatchQueue.main.async).

like image 188
Jack Goossen Avatar answered Nov 15 '22 06:11

Jack Goossen