title: Why do I get the error @Composable invocations can only happen from the context of a @Composable fun when I use LaunchedEffect in Jetpack Compose?
body: <p>There are two <code>MutableState</code> variables, <code>currentInfo</code> and <code>maxInfo</code>.
  <code>currentInfo</code> is updated frequently, and <code>maxInfo</code> is updated accidentally.</p>
  <p>The Code A can work, but I think that the efficiency of code is not good because <code>Text(text = &quot;Max ${handleMeter.maxInfo}&quot;)</code> will be launhced  frequently when <code>currentInfo</code> is updated .</p>
  <p>I read the <a href="https://developer.android.com/jetpack/compose/side-effects" rel="nofollow noreferrer">article</a>, so I hope Code B to improve the efficiency, but Code B cause the following error?</p>
  <p><em>@Composable invocations can only happen from the context of a @Composable fun</em></p>
  <p>1: How can I fixed the problem?</p>
  <p>2: In the Case, do I need to consider improve the efficiency ?  or can the system optimize UI recompose automatically to reduce <code>Text(text = &quot;Max ${handleMeter.maxInfo}&quot;)</code> launhced ?</p>
  <p><strong>Code A</strong></p>
  <pre><code>@Composable
  fun Greeting(handleMeter: HandleMeter) {
      Column(
          modifier = Modifier.fillMaxSize(),
      ) {
  
          Text(text = &quot;Current ${handleMeter.currentInfo}&quot;)
          Text(text = &quot;Max ${handleMeter.maxInfo}&quot;)
      }
  }
  
  
  class HandleMeter: ViewModel() {
      var currentInfo by mutableStateOf(2.0)
      var maxInfo by mutableStateOf(10.0)
  
       ...
  }
  </code></pre>
  <p><strong>Code B</strong></p>
  <pre><code>@Composable
  fun Greeting(handleMeter: HandleMeter) {
      Column(
          modifier = Modifier.fillMaxSize(),
      ) {
  
          Text(text = &quot;Current ${handleMeter.currentInfo}&quot;)
          LaunchedEffect(handleMeter.maxInfo) {
              Text(text = &quot;Max ${handleMeter.maxInfo}&quot;)
          }
  
      }
  }
  
  //The same
  </code></pre>