如何在React Native中找到状态变量的长度?

时间:2019-01-15 14:38:51

标签: react-native native-base

class App extends React.Component {
constructor(props) {
    super()
    this.state = {
        count: 0,
        cicked: false
    }
    this.handelClick = this.handelClick.bind(this)
}
handelClick() {

    this.setState({
        count: this.state.count + 1,
        clicked: !this.state.clicked,
    })
    console.log(this.state.count)
}

render() {
    function getRandom(max) {
        return Math.floor(Math.random() * Math.floor(max));
    }


return (
    <div className="body">
        <Questions Number1={getRandom(10)} Number2={getRandom(10)} />
        <Button click={this.handelClick} choice={(this.props.Number1 * this.props.Number2)} />

  </div>
);

我想查找处于更改状态的phone变量的长度,以便如果长度为10,我就可以执行操作。

如果可能的话,您还可以让我知道如何在React Native中以编程方式启用和禁用基于Native的按钮。

我是React Native的初学者

1 个答案:

答案 0 :(得分:0)

获取字符串长度

如果电话号码另存为字符串,则非常简单。你可以做

let phoneNumberLength = this.state.phone.length

然后您可以在if语句中使用它

if (phoneNumberLength === 10) {
  // do something here
}

本地基地

nativebase中的Button组件具有可以使用的禁用道具。 http://docs.nativebase.io/Components.html#button-disabled-headref

您可以在状态中设置一个值来控制按钮是否被禁用。调用this.state({buttonDisabled: true})将禁用该按钮

this.state {
  buttonDisabled: false
}

<Button disabled={this.state.buttonDisabled} />