function - 打开 native 屏幕时调用函数

标签 function react-native react-navigation

每次用户打开我的 react-native 屏幕之一(我使用的是 StackNavigator)时,我都会尝试从 AsyncStorage 加载 JSON。此 JSON 包含有关我的状态应设置为什么的信息。

如何调用每次打开此屏幕时运行的函数?

更多信息:
我编写了一个函数,根据从 AsyncStorage 加载的 JSON 更新我的状态。从按钮调用该函数时可以完美运行,但是当从 render() 调用该函数时,我的部分屏幕会卡住并且某些按钮不再可触摸。奇怪的是只有 TextInput 仍然有效。

最佳答案

使用componentWillMount()方法。这将在 render() 方法被触发之前自动执行。

class Sample extends Component{
    state = {data : []};
    componentWillMount(){
        this.setState({data : inputObject});
    }
    render(){
        return(
            <View>
            //you can render the data here
            </View>
        );
    }
}

引用:https://facebook.github.io/react/docs/react-component.html#componentwillmount

关于function - 打开 native 屏幕时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44807106/

相关文章:

c - 如何修改已传递给 C 函数的指针?

react-native - 如何调试 babel.config.js

reactjs - 当 react 导航中的 'navigationRef' 准备好时,我如何监听?

javascript - 如何从 react 导航中删除默认样式?

python - Python 3's "函数注释有什么用?

javascript - 在 WebView 中调用 JavaScript 函数

javascript - ReactNative - 无法从 native 模块调用函数

android - ToolbarAndroid 未显示在 React Native 中

react-native - react native 纸主题与 react 导航不起作用

Javascript:如何延迟内容功能