javascript - 构建介绍,阅读App本地设置

标签 javascript react-native redux

我对react-native完全陌生,正在尝试构建我的第一个应用程序。

问题是我想为应用程序制作介绍,并且我决定使用react-native-app-intro。一旦用户通过了应用程序介绍,我将在 Redux 存储中将firstRun 状态更改为 false。为了将设置持久保存到设备,我使用 redux-persist。

我只是不明白下次如何跳过介绍。对于导航,我使用 react 导航。我已经对介绍场景做了 if 语句

// Intro
...
render(){
if (this.props.settings.firstRun === false){
   this.props.navigation.navigate('Home');
}
...

这可行,但是有更好的方法吗?由于 redux 存储初始化应用程序将始终获得我的firstRun初始设置:true。

// my settings reducer   
export default function(state = settingsInitState, action) {
switch(action.type){
    case 'SETTINGS_CHANGE':
        state.firstRun = action.payload.value;
        return state;
        break;
}
return state;
}

const settingsInitState = {
        firstRun: true,
};
// actions
export const changeSettings = (settings) => {
return {
    type: settings.type,
    payload: settings
}
}

我应该在reducer设置初始化中跳过@@redux/INIT和@@redux/PROBE状态吗?

有人可以举例说明如何处理应用程序的介绍吗?

最佳答案

不确定回复:redux-persist,但您可以在“启动场景”中执行此操作(基本上只是一个带有背景颜色和/或 Logo 图像的空 View )作为您的第一个事件,并使用 AsyncStorage如:

import {AsyncStorage, ...} from 'react-native'
const KEY_SeenStart = 'seenStartKey';

class SplashScreen extends Component {
    constructor(props) {
        super(props);
        this.state = {
            id: 'splash screen',
            seenStart: 'false',
        };
    }

componentDidMount() {
 AsyncStorage.getItem(KEY_SeenStart).then((seenIntro) => {
    if (seenIntro !== null) {  //has already seen app intro
        this.setState({seenStart: seenIntro});//use this to direct one way or another...
    }else{    //hasn't seen app intro...
        this.setState({seenStart: 'false'});
        try {
            AsyncStorage.setItem(KEY_SeenStart, 'true');//has seen it now, set 'true'
        } catch (error) {
            window.alert('AsyncStorage error: ' + error.message);
        }
    }
});

var whereToGo = (this.state.seenStart == 'true')?'first scene':'app intro';
        this.props.navigator.replace({
            id: whereToGo,
            passProps: {
                ...
                }
        });
  }
...
}

启动场景也是进行任何网络连接的好地方,然后您可以将结果作为 Prop 传递给您的“第一个”事件。希望这有帮助!

关于javascript - 构建介绍,阅读App本地设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42983132/

相关文章:

react-native - 无法读取未定义的属性 'openPicker'

javascript - React Native - 世博会 : fontFamily 'SimpleLineIcons' is not a system font and has not been loaded through Font. loadAsync

reactjs - [RN][Redux-Persist] AutoReHydrate 不是一个函数

javascript - 使用选择器获取相关项目

javascript - 使用 props.change 更改 redux-form 中输入字段的值

javascript - 用于网络摄像头视频捕获的 Red5 录像机和服务器的简单替代品?

javascript - 使用 JQuery 在表中显示 JSON

ios - React-Native with Redux : API objects appear in my console. 日志但不是我的观点

javascript - 如何使用 Plyr JS 在视频播放器中添加下载按钮?

javascript - p5.j​​s 中的噪声梯度