当前位置: 首页 > 知识库问答 >
问题:

在React Native中更改其他组件的状态

高修伟
2023-03-14

我是个新来的土生土长的人。我有一个屏幕,它将根据其当前状态进行渲染,如下所示。默认情况下(初始状态),它将呈现登录屏幕。App.js

import React, { Component } from 'react';
import { View, TouchableOpacity, Text } from 'react-native';
import { Header } from './components/Export';
import LoginBox from './components/LoginBox';
import AdditionalActivity from './components/AdditionalActivity';
import SignUp from './components/SignUp';


export default class App extends Component {

  state = {
    status: 'initial'
  }

  renderBasedOnLoggedInState() {
    if(this.state.status == 'initial') {
      return (
        <View>
          <Header headerText="APP NAME"/>
          <LoginBox/>
          <AdditionalActivity />
        </View>
      );
    } else if (this.state.status == 'logged') {
      return (
        <View>
          <Text>Welcome to my application</Text>
        </View>
      )
    } else {
      return (
        <View>
          <Header headerText="APP NAME"/>
          <SignUp/>
          <AdditionalActivity />
        </View>
      )
    }
  }

  render() {
    return (
      <View>
        {this.renderBasedOnLoggedInState()}
      </View>
    );
  }
}

当下面的登录成功后,我需要将组件应用程序的状态从“初始”更改为“已登录”。我怎么做呢?这里的登录函数仅用于测试目的,所以不必太在意它。LoginBox.js

import React, { Component } from 'react'
import { Alert, Text, View, TouchableOpacity, Button } from 'react-native'
import GreyTextbox from './GreyTextbox'


export default class LoginBox extends Component {
    state = {
        username: '',
        password: '',
    }

    Login()
    {    
        var username = this.state.username;
        var password = this.state.password;
        var userdata = require('./a.json');

        var count = 0;
        for (let j = 0; j < 2; j++) {
            if ((userdata[j].username == username) && ( userdata[j].password == password))
            {
                Alert.alert("true");    
                count++;
            }             
        }   
        if(count == 0)
        {
            Alert.alert("false");   
        }
    }

    render() {
        return (
            <View style={styles.containerStyle}>
                <GreyTextbox
                    secureOption={false}
                    placeholder="username"
                    value={this.state.username}
                    onChangeText={username => this.setState({username})}
                />
                <GreyTextbox
                    secureOption={true}
                    placeholder="password"
                    value={this.state.password}
                    onChangeText={password => this.setState({password})}
                />
                <TouchableOpacity
                    style={styles.buttonStyle}
                    onPress={this.Login.bind(this)} >                   >
                    <Text style={styles.textStyle}>Log In</Text>
                </TouchableOpacity>
            </View>
        )
    }
}

const styles = {
    containerStyle: {
        //flex: 0.35,
        height: 180,
        justifyContent: 'space-between',
        marginTop: 40,
        marginLeft: 30,
        marginRight: 30,
        position: 'relative'
    },
    buttonStyle: {
        borderWidth: 1,
        borderColor: '#3da8ff',
        alignContent: 'center',
        justifyContent: 'center',
        marginLeft: 25,
        marginRight: 25,
        paddingTop: 5,
        paddingBottom: 5,
    },
    textStyle: {
        color: '#3da8ff',
        alignSelf: 'center',
        fontSize: 20
    }
}

共有1个答案

黄俊誉
2023-03-14

创建一个更改状态的方法,并将其作为prop传递给子组件。

export default class App extends Component {
  constructor(props) {
    super(props)
    this.state = {status: 'initial'}
    this.changeState = this.changeState.bind(this)
  }

  changeState() {
    this.setState({
      status: 'logged'
    })
  }

  render() {
    return (
      <View>
        <Header headerText="APP NAME"/>
          <LoginBox changeState = {this.changeState}/>
        <AdditionalActivity />
      </View>
  }
}

在LoginBox.js中,您可以通过道具调用它:

class LoginBox extends Component {

  constructor(props) {
    super(props);
    this.login = this.login.bind(this);
}

  login() {    
    this.props.changeState;
  }

  render() {
    return (
      {...}
      <TouchableOpacity
        style={styles.buttonStyle}
        onPress={this.login} >                   >
          <Text style={styles.textStyle}>Log In</Text>
      </TouchableOpacity>
    )
  }
}

另一个有用的提示:永远不要在渲染方法中绑定函数!

 类似资料:
  • 控制器类别:包装样品;

  • 这个问题在这里已经得到了回答,但事情总是在变化。 基本上,父组件获取一些数据,子组件需要这些数据。 这是子组件。

  • 我正在开发一个带有样式组件的反应应用程序,我有一个组件“导航”。在这个组件中有更多的组件,如,等。Header例如是这样声明的: 问题是,我在不同的文件中有这个导航组件,对于所有文件,样式都很好,但现在我只想在其中一个文件中更改标题组件的背景色,它位于(?)导航组件。我该怎么做?我知道可以用const NewNav=styled(导航)之类的东西从导航组件更改样式,`但是标题呢? 先谢谢你。

  • 我有两个组件:父组件,我想改变子组件的状态: 和子组件: 我需要从父组件更改子组件的打开状态,或者当单击父组件中的按钮时,从父组件调用子组件的toggleMenu()?

  • 我创建了一个包含登录组件和注册组件的签名页面组件。我的问题是,我如何传递一个onhtml函数从父到子,以便更改我的父组件(签名页组件)的状态?我期望的是,通过单击子组件中的按钮,可以切换父组件的活动状态,并根据活动状态更改样式。我试图通过一个处理功能从父到子,但它不能很好地工作。 有什么解决办法吗? codesandbox中的详细信息:https://codesandbox.io/s/wizard

  • 我目前有一个类组件和一个函数组件在我的主组件 以下是的代码段: 类组件 功能部件 因此,我想做的是通过按钮的onClick操作,从功能组件更改类组件的状态。我怎样才能完成任务?