菜鸟教程小白 发表于 2022-12-13 02:12:23

javascript - 按钮未出现


                                            <p><p><strong>注意:我是 React Native 的新手。</strong>下面的代码应该是使用 React Native 运行的计算器。我在显示此计算器代码的按钮时遇到问题。代码运行时没有报错,所以不明白为什么没有出现按钮。</p>

<p>代码如下:</p>

<pre><code>import React, { Component } from &#39;react&#39;;
import {
    View,
    Text,
    AppRegistry,
    StyleSheet,
} from &#39;react-native&#39;;

const inputButtons = [
    ,
    ,
    ,
   
];


const Style = StyleSheet.create({
    rootContainer: {
      flex: 1
    },

    displayContainer: {
      flex: 2,
      backgroundColor: &#39;#193441&#39;
    },

    inputContainer: {
      flex: 8,
      backgroundColor: &#39;#3E606F&#39;
    },

    inputButton: {
      flex: 1,
      alignItems: &#39;center&#39;,
      justifyContent: &#39;center&#39;,
      borderWidth: 0.5,
      borderColor: &#39;#91AA9D&#39;
    },

    inputButtonText: {
      fontSize: 22,
      fontWeight: &#39;bold&#39;,
      color: &#39;white&#39;
    },
    inputRow: {
      flex: 1,
      flexDirection: &#39;row&#39;
    }
});
&lt;View style={Style.rootContainer}&gt;
    &lt;View style={Style.displayContainer}&gt;&lt;/View&gt;
    &lt;View style={Style.inputContainer}&gt;&lt;/View&gt;
&lt;/View&gt;

export default class ReactCalculator extends Component {

   render() {
         return (
            &lt;View style={Style.rootContainer}&gt;
                &lt;View style={Style.displayContainer}&gt;&lt;/View&gt;
                &lt;View style={Style.inputContainer}&gt;
                  {this._renderInputButtons()}
                &lt;/View&gt;
            &lt;/View&gt;
      )
    }
    _renderInputButtons() {
      let views = [];

      for (var r = 0; r &lt; inputButtons.length; r ++) {
            let row = inputButtons;

            let inputRow = [];
            for (var i = 0; i &lt; row.length; i ++) {
                let input = row;

                inputRow.push(
                  &lt;InputButton value={input} key={r + &#34;-&#34; + i} /&gt;
                );
            }

            views.push(&lt;View style={Style.inputRow} key={&#34;row-&#34; + r}&gt;{inputRow}&lt;/View&gt;)
      }

      return views;
    }

    render() {
    return (
      &lt;View style={{flex: 1}}&gt;
            &lt;View style={{flex: 2, backgroundColor: &#39;#193441&#39;}}&gt;&lt;/View&gt;
            &lt;View style={{flex: 8, backgroundColor: &#39;#3E606F&#39;}}&gt;&lt;/View&gt;
      &lt;/View&gt;
    )

}


}
</code></pre>

<p><strong>最新代码:</strong> - 当我没有收到错误时收到错误,按钮出现在不正确的位置。</p>

<pre><code>import React, { Component } from &#39;react&#39;;
import {
    View,
    Text,
    AppRegistry,
    StyleSheet,
    Button,
    TouchableHighlight,
} from &#39;react-native&#39;;

const inputButton = [
    ,
    ,
    ,
   
];


const Style = StyleSheet.create({
    rootContainer: {
      flex: 1
    },

    displayContainer: {
      flex: 2,
      backgroundColor: &#39;#193441&#39;
    },

    inputContainer: {
      flex: 8,
      backgroundColor: &#39;#3E606F&#39;
    },

    inputButton: {
      flex: 1,
      alignItems: &#39;center&#39;,
      justifyContent: &#39;center&#39;,
      borderWidth: 0.5,
      borderColor: &#39;#91AA9D&#39;
    },

    inputButtonText: {
      fontSize: 22,
      fontWeight: &#39;bold&#39;,
      color: &#39;white&#39;
    },
    inputRow: {
      flex: 1,
      flexDirection: &#39;row&#39;
    }
});
&lt;View style={Style.rootContainer}&gt;
    &lt;View style={Style.displayContainer}&gt;&lt;/View&gt;
    &lt;View style={Style.inputContainer}&gt;&lt;/View&gt;
&lt;/View&gt;

export default class ReactCalculator extends Component {

   render() {
         return (
            &lt;TouchableHighlight style={Style.inputButton}
                              underlayColor=&#34;#193441&#34;
                              onPress={this.props.onPress}&gt;
                &lt;Text style={Style.inputButtonText}&gt;{this.props.value}&lt;/Text&gt;
            &lt;/TouchableHighlight&gt;
      )
    }
    _renderInputButton() {
    let views = [];

    for (var r = 0; r &lt; inputButton.length; r ++) {
      let row = inputButton;

      let inputRow = [];
      for (var i = 0; i &lt; row.length; i ++) {
            let input = row.toString();

            inputRow.push(
            &lt;InputButton
                value={input}
                onPress={this._onInputButtonPressed.bind(this, input)}
                key={r + &#34;-&#34; + i}/&gt;
      );
    }

    _onInputButtonPressed(input) {
      alert(input)
    }

      views.push(&lt;View style={Style.inputRow} key={&#34;row-&#34; + r}&gt;{inputRow}&lt;/View&gt;)
    }

    return views;
}



}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在你的代码上我发现了一些问题:</p>

<p><strong>1.</strong> 方法 <code>this._renderInputButton()</code> 未定义,因为当您声明该方法时,您会编写 <code>_renderinputButton()</code>。您必须调用具有相同名称的方法。 (React Native 区分大小写)</p>

<p><strong>2.</strong> 我没有在您的代码中找到组件 <code>InputButton</code>。您如何创建组件?</p>

<p>我认为你的代码不能这样工作的问题。也许你可以告诉我你在哪里得到代码。</p>

<p><em>编辑</em>#1</p>

<p>您可以创建 <code>InputButton</code> 与文件 <code>index.js</code> 分开。然后写入文件 <code>InputButton.js</code> :</p>

<pre><code>import React, { Component } from &#39;react&#39;;
import {
    View,
    Text,
    StyleSheet
} from &#39;react-native&#39;;

const Style = StyleSheet.create({
   inputButton: {
         flex: 1,
         alignItems: &#39;center&#39;,
         justifyContent: &#39;center&#39;,
         borderWidth: 0.5,
         borderColor: &#39;#91AA9D&#39;,
   },
   inputButtonText: {
         fontSize: 22,
         fontWeight: &#39;bold&#39;,
         color: &#39;white&#39;,
   },
   });

export default class InputButton extends Component {

render() {
      return (
          &lt;View style={Style.inputButton}&gt;
            &lt;Text style={Style.inputButtonText}&gt;{this.props.value}&lt;/Text&gt;
      &lt;/View&gt;
    )
}

}
</code></pre>

<p>你可以在 index.js 文件中添加 <code>import InputButton from './InputButton'</code> 像:</p>

<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>import React, { Component } from &#39;react&#39;;
import { View, Text, AppRegistry, StyleSheet } from &#39;react-native&#39;;
import InputButton from &#39;./InputButton&#39;;

const inputButton = [
,
,
,
,
];

const Style = StyleSheet.create({
rootContainer: {
    flex: 1,
},

displayContainer: {
    flex: 2,
    backgroundColor: &#39;#193441&#39;,
},

inputContainer: {
    flex: 8,
    backgroundColor: &#39;#3E606F&#39;,
},

inputButton: {
    flex: 1,
    alignItems: &#39;center&#39;,
    justifyContent: &#39;center&#39;,
    borderWidth: 0.5,
    borderColor: &#39;#91AA9D&#39;,
},

inputButtonText: {
    fontSize: 22,
    fontWeight: &#39;bold&#39;,
    color: &#39;white&#39;,
},
inputRow: {
    flex: 1,
    flexDirection: &#39;row&#39;,
},
});

export default class routerFlax extends Component {
_renderInputButton() {
    let views = [];

    for (var r = 0; r &lt; inputButton.length; r++) {
      let row = inputButton;

      let inputRow = [];
      for (var i = 0; i &lt; row.length; i++) {
      let input = row;

      inputRow.push(&lt;InputButton value={input} key={r + &#39;-&#39; + i} /&gt;);
      }

      views.push(
      &lt;View style={Style.inputRow} key={&#39;row-&#39; + r}&gt;{inputRow}&lt;/View&gt;,
      );
    }

    return views;
}

render() {
    return (
      &lt;View style={Style.rootContainer}&gt;
      &lt;View style={Style.displayContainer} /&gt;
      &lt;View style={Style.inputContainer}&gt;
          {this._renderInputButton()}
      &lt;/View&gt;
      &lt;/View&gt;
    );
}
}</code></pre>

                                                <p style="font-size: 20px;">关于javascript - 按钮未出现,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45154539/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45154539/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: javascript - 按钮未出现