React Native - 编辑其他组件的状态

艾哈迈德·尼扎尔

我想通过运行检查应用程序当前状态是否处于活动状态的函数来编辑其他组件的状态,这意味着每次应用程序进入前台时它都会运行。

let d = new Date().getHours();
let title = messages[`_${d}`].message;

function getAppState() {
  AppState.addEventListener("change", (state) => {
    if(state == "active") {
      d = new Date().getHours();
      title = messages[`_${d}`].message;
      // App.setState({ text: title }); **Not working!
      console.log(title);
    }
  });
}

export default class App extends React.Component {
  constructor() {
    super();
    this.state = { text: title };
    getAppState();
  }
  render() {
    return (
      <View>
        <StatusBar hidden={ true } />
        <Text style={styles.title}>{title}</Text>
      </View>
    );
  }
}

我想根据小时更改文本的值。

混搭

我没有一个环境来测试这个,但我会这样做:

export default class App extends React.Component {
  constructor() {
    super();
    this.state = {
      text: title
    };

    // Bind updateTile() so `this` corresponds tho the react component and things
    // like `this.setState()` are available. Otherwise `this` would be `AppState`
    this.updateTitle = this.updateHour.bind(this);

  }

  componentWillMount() {
    // Listen for the event
    AppState.addEventListener("change", this.updateTitle);
  }

  updateTitle(state) {
    if (state == "active") {
      const d = new Date().getHours();
      const title = messages[`_${d}`].message;
      this.setState({
        text: title
      });
      console.log(title);
    }
  }

  render() {
    return (
        <View >
        <StatusBar hidden={true} />
        <Text style = {styles.title}>{title}</Text>
      </View >
    );
  }
}

如果你想updateTitle()成为另一个功能而不是Component我的一部分,我会这样做:

const updateComponentTitle = state => {
  if (state == "active") {
    const d = new Date().getHours();
    const title = messages[`_${d}`].message;
    this.setState({
      text: title
    });
    console.log(title);
  }
}


export default class App extends React.Component {
  constructor() {
    super();
    this.state = {
      text: title
    };

    // Bind updateTile() so `this` corresponds tho the react component and things
    // like `this.setState()` are available. Otherwise `this` would be `AppState`
    this.updateTitle = udpateComponentTitle.bind(this);

  }

  componentWillMount() {
    // Listen for the event
    AppState.addEventListener("change", this.updateTitle);
  }

  render() {
    return (
        <View >
        <StatusBar hidden={true} />
        <Text style = {styles.title}>{title}</Text>
      </View >
    );
  }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React Native:从其他组件更改状态

来自分类Dev

如何从其他组件 React Native 更新主组件状态

来自分类Dev

React Native无法从其他组件的函数体内更新组件

来自分类Dev

React Native,如何使用其他文件的状态?

来自分类Dev

如何在 React Native 上以干净的方式与其他组件共享组件逻辑?

来自分类Dev

在react-native的其他组件中找不到变量导航

来自分类Dev

React Native:不可重用组件的this.state状态

来自分类Dev

React Native:组件状态下的重复项

来自分类Dev

React Native:不可重用组件的this.state状态

来自分类Dev

更改视频 react-native-video 组件中的状态/道具

来自分类Dev

React Native 的组件 render() 中未保留状态

来自分类Dev

React Native 组件不会随着状态变化而更新

来自分类Dev

传递更新状态React Native

来自分类Dev

React Native不更新状态

来自分类Dev

React Native 忽略状态变化

来自分类Dev

React Native:创建组件错误

来自分类Dev

通过React Native覆盖组件

来自分类Dev

React Native什么是<>(空)组件

来自分类Dev

如何覆盖React Native组件?

来自分类Dev

React Native组件上的图像

来自分类Dev

React Native中的组件异常

来自分类Dev

创建动态组件 React (Native)

来自分类Dev

React.js如何从其他组件更改组件的状态

来自分类Dev

React native中的react-native-fs库是否有其他可用的替代方法?

来自分类Dev

react native-用react钩子通知子组件中状态更新的父组件?

来自分类Dev

React Native如何从其他功能更新UI

来自分类Dev

来自其他应用的 React Native 深层链接

来自分类Dev

将视图放在 React Native 中的其他视图之上

来自分类Dev

基于其他回调函数的函数... react-native

Related 相关文章

热门标签

归档