React Native | 函数不更新变量

huangapple go评论102阅读模式
英文:

React Native | A function does not update variables

问题

I am currently programming a mobile app with which you can check whether a Minecraft server is online or not. Actually everything works but the variables in which I write the output are not updated.
Can someone please help me?

My Code:

import { StyleSheet, Text, Image, Button, View } from 'react-native';

export default function App() {
  var error
  var serverIpNew
  var motd
  var playerCount

  function initServerData(serverIp, serverPort){
    if(serverIp == "127.0.0.1"){
        var serverIpNew = "Name";
    } else {
        if(serverIp == "127.0.0.1"){
            var serverIpNew = "Name";
        } else {
            var serverIpNew = serverIp;
        }
    }

    if(serverPort == null){
        var serverPortNew = 25565;
    } else {
        var serverPortNew = serverPort;
    }

    fetch('https://mcapi.us/server/status?ip='+serverIp+'&port='+ serverPortNew)
    .then(response => response.json())
    .then(data => handleServerStatus(data) );

    function handleServerStatus(data){
        if(data.status=='error'){
            console.log(data.error);
            var error = 'ERROR | The Server does not exist or the Server is Offline <br> Server IP: <br>' + serverIpNew + ' (' + serverIp + ')';
            return false;
        }

        var motd = data.motd;
        var playerCount = data.players.now + "/" + data.players.max;
    } 
  }

  return (
    <View style={styles.container}>
      <Button
        title="Press me"
        onPress={initServerData('127.0.0.1', '25565')}
      />
      <View class="server_outline">
        <View class="server" id="server">
          <Text>{error}</Text>
              <Image
                style={styles.tinyLogo}
                source={{
                  uri: 'https://img.freepik.com/vetores-gratis/glitch-error-404-page_23-2148105404.jpg',
                }}
                id="server-icon"
                class="icon"
              />
              <View class="info">
                <Text id="server-ip">{serverIpNew}</Text>
                <Text id="motd">{motd}</Text>
                <View class="player-count">
                    <Text id="player-count">{playerCount}</Text>
                    <Text>Players are Online</Text>
                </View>
            </View>
        </View>
      </View>
    </View>
  );
}

Some text that I can ask this question:

Lorem ipsum dolor sit amet. Aut esse sapiente in ullam officiis vel quas voluptatem eum sint nesciunt id sapiente omnis? Quo eveniet vero eos voluptatem perspiciatis 33 culpa quia non deleniti minus aut architecto facilis ab explicabo voluptatem et earum sint. Qui ipsa molestiae cum sapiente voluptatem in voluptas iusto ut inventore sunt vel dolorem sapiente est consequatur consequatur. Vel nemo assumenda et dolor sequi ea voluptatem suscipit vel omnis error ut amet iure eum facere maiores.

Quo veritatis iusto qui nisi modi aut explicabo nobis ab voluptatum quod. Vel voluptatibus consequuntur est voluptas natus vel culpa provident sit dignissimos dolorem et explicabo atque id internos dolor id quis quos! Aut voluptate laudantium sed aliquid illum sed Quis quia ad consequatur minus sed similique doloremque id nihil praesentium ut laborum voluptatum. Qui quisquam ipsum in quia tempora aut quia quia et eius modi et illo dolorum sit fugit consequatur ab alias quia!

Ad libero corporis et itaque rerum et doloremque quasi ut esse rerum. Aut dolorem perspiciatis qui odit distinctio non praesentium omnis. Eum dolores laudantium ea incidunt atque in sequi veritatis quo aliquam facilis eum ducimus architecto id nesciunt ducimus et ipsa corporis. Et tempore minus est galisum totam ab molestiae sequi.

英文:

I am currently programming a mobile app with which you can check whether a Minecraft server is online or not. Actually everything works but the variables in which I write the output are not updated.
Can someone please help me?

My Code:

import { StyleSheet, Text, Image, Button, View } from &#39;react-native&#39;;
export default function App() {
var error
var serverIpNew
var motd
var playerCount
function initServerData(serverIp,serverPort){
if(serverIp == &quot;127.0.0.1&quot;){
var serverIpNew = &quot;Name&quot;;
} else {
if(serverIp == &quot;127.0.0.1&quot;){
var serverIpNew = &quot;Name&quot;;
} else {
var serverIpNew = serverIp;
}
}
if(serverPort == null){
var serverPortNew = 25565;
} else {
var serverPortNew = serverPort;
}
fetch(&#39;https://mcapi.us/server/status?ip=&#39;+serverIp+&#39;&amp;port=&#39;+ serverPortNew)
.then(response =&gt; response.json())
.then(data =&gt; handleServerStatus(data) );
function handleServerStatus(data){
if(data.status==&#39;error&#39;){
console.log(data.error);
var error = &#39;ERROR | The Server does not exist or the Server is Offline &lt;br&gt; Server IP: &lt;br&gt;&#39; + serverIpNew + &#39; (&#39; + serverIp + &#39;)&#39;;
return false;
}
var motd = data.motd;
var playerCount = data.players.now + &quot;/&quot; + data.players.max;
} 
}
return (
&lt;View style={styles.container}&gt;
&lt;Button
title=&quot;Press me&quot;
onPress={initServerData(&#39;127.0.0.1&#39;, &#39;25565&#39;)}
/&gt;
&lt;View class=&quot;server_outline&quot;&gt;
&lt;View class=&quot;server&quot; id=&quot;server&quot;&gt;
&lt;Text&gt;{error}&lt;/Text&gt;
&lt;Image
style={styles.tinyLogo}
source={{
uri: &#39;https://img.freepik.com/vetores-gratis/glitch-error-404-page_23-2148105404.jpg&#39;,
}}
id=&quot;server-icon&quot;
class=&quot;icon&quot;
/&gt;
&lt;View class=&quot;info&quot;&gt;
&lt;Text id=&quot;server-ip&quot;&gt;{serverIpNew}&lt;/Text&gt;
&lt;Text id=&quot;motd&quot;&gt;{motd}&lt;/Text&gt;
&lt;View class=&quot;player-count&quot;&gt;
&lt;Text id=&quot;player-count&quot;&gt;{playerCount}&lt;/Text&gt;
&lt;Text&gt;Players are Online&lt;/Text&gt;
&lt;/View&gt;
&lt;/View&gt;
&lt;/View&gt;
&lt;/View&gt;
&lt;/View&gt;
);
}

Some text that I can ask this question:

Lorem ipsum dolor sit amet. Aut esse sapiente in ullam officiis vel quas voluptatem eum sint nesciunt id sapiente omnis? Quo eveniet vero eos voluptatem perspiciatis 33 culpa quia non deleniti minus aut architecto facilis ab explicabo voluptatem et earum sint. Qui ipsa molestiae cum sapiente voluptatem in voluptas iusto ut inventore sunt vel dolorem sapiente est consequatur consequatur. Vel nemo assumenda et dolor sequi ea voluptatem suscipit vel omnis error ut amet iure eum facere maiores. </p><p>Quo veritatis iusto qui nisi modi aut explicabo nobis ab voluptatum quod. Vel voluptatibus consequuntur est voluptas natus vel culpa provident sit dignissimos dolorem et explicabo atque id internos dolor id quis quos! Aut voluptate laudantium sed aliquid illum sed Quis quia ad consequatur minus sed similique doloremque id nihil praesentium ut laborum voluptatum. Qui quisquam ipsum in quia tempora aut quia quia et eius modi et illo dolorum sit fugit consequatur ab alias quia! </p><p>Ad libero corporis et itaque rerum et doloremque quasi ut esse rerum. Aut dolorem perspiciatis qui odit distinctio non praesentium omnis. Eum dolores laudantium ea incidunt atque in sequi veritatis quo aliquam facilis eum ducimus architecto id nesciunt ducimus et ipsa corporis. Et tempore minus est galisum totam ab molestiae sequi.

答案1

得分: 1

你应该了解React生命周期。如果你想保留你设置的值,你应该使用useState函数。要了解更多信息,请参阅以下文档:https://react.dev/learn/managing-state

英文:

You should read about react lifecycle. You should useState function if you want preserve the values that you set. To know more about, here you have documentation: https://react.dev/learn/managing-state

huangapple
  • 本文由 发表于 2023年8月10日 23:21:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定