What does the SwiftUI `@State` keyword do?

前端 未结 5 1711
后悔当初
后悔当初 2020-12-08 13:34

The SwiftUI tutorial uses the @State keyword to indicate mutable UI state:

@State var showFavoritesOnly = false

It offers this summ

5条回答
  •  抹茶落季
    2020-12-08 14:20

    Let me add something else if you know React Native.

    The @State property is very like the this.state object in React Native.

    For example:

    struct Foobar: some View {
        @State var username = ""
    }
    
    class Foobar extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          username: '',
        };
      }
    }
    

    When you modify the username variable, they will have the same effect, that re-render the current page.

提交回复
热议问题