简体   繁体   English

如何在React Native页面中安排状态更改?

[英]How can I orchestrate state change in my React Native page?

I wish to have a number (say 10) different 'pages' on a React Native screen. 我希望在React Native屏幕上有多个(例如10个)不同的“页面”。 The use case is a workflow or flowchart. 用例是工作流程或流程图。 Each page can send you to multiple different places. 每个页面都可以将您发送到多个不同的地方。

I want the final thing to be flexible enough that I don't want to simply write the 10 or so .js pages and link them together. 我希望最后一件事足够灵活,以至于我不想简单地编写10个左右的.js页并将它们链接在一起。

My cunning plan was to put a state object in the 'state' thing that React provides, I am now stumped as to how to update this object. 我的狡猾计划是将状态对象放入React提供的“状态”对象中,现在我对如何更新此对象感到困惑。

This is my current code. 这是我当前的代码。 renderif is a bit of magic that hides the following layout if it is passed false renderif是一个魔术,如果传递了false它将隐藏以下布局

The current behaviour is it shows 'page one' only. 当前行为是仅显示“第一页”。 when I press the button I see a blank screen. 当我按下按钮时,我看到一个空白屏幕。

What basic javascript errors am I making? 我犯了哪些基本的JavaScript错误? and how have I over complexified this for myself :) 以及我如何为自己过度复杂化这一点:)

import React, {Component} from "react";
import {StyleSheet, Text, View, ScrollView, Image, Button} from "react-native";
import {Font, AppLoading, WebBrowser} from "expo";
import {Actions} from "react-native-router-flux";
import styles from "./Styles";
import renderif from "./RenderIf";

class pageToShow {
  constructor() {
    this.GoToPageOne();
  }
  GoToPageOne() {
    this.pageOne = true;
    this.pageTwo = false;
    return this;
  }
  GoToPageTwo() {
    this.pageOne = false;
    this.pageTwo = true;
    return this;
  }
}
export default class FlipBook extends Component {
  constructor(props) {
    super(props);
    this.state = {show: new pageToShow()};
  }
  render() {
    return (
      <View>
        {renderif(this.state.show.pageOne)(
          <ScrollView style={styles.background}>

            <Text style={styles.header}>
              <Text>{"Page one"}</Text>
            </Text>
            <Text style={styles.normal}>
              This is page one
            </Text>
            <Button
              title="This is a button to two"
              onPress={() => this.setState({show: this.state.show.GoToPageTwo})}
            />
          </ScrollView>
        )}
        {renderif(this.state.show.pageTwo)(
          <ScrollView style={styles.background}>
            <Text style={styles.header}>
              <Text>{"Page two"}</Text>
            </Text>
            <Text style={styles.normal}>
              This is page two
            </Text>
            <Button
              title="This is a button to one"
              onPress={() => this.setState({show: this.state.show.GoToPageOne})}
            />
          </ScrollView>
        )}
      </View>
    );
  }
}

Hmm i think you're making this way more complicated than it should be. 嗯,我认为您正在使这种方式比应有的方式更加复杂。 For one you can use a navigation library to navigate between the pages, but if you want everything to be handled by state you could do something like this. 对于一个人,您可以使用导航库在页面之间导航,但是如果您希望所有内容都由状态处理,则可以执行以下操作。 *Also you could use 'case' to handle conditional rendering too: *此外,您也可以使用“ case”来处理条件渲染:

export default class FlipBook extends Component {
  constructor(props) {
    super(props);
    this.state = {show: 'pageOne' };
  }
  render() {
    return (
      <View>
        {this.state.show === 'pageOne' ? (
          <ScrollView style={styles.background}>
            <Text style={styles.header}>
              <Text>{"Page one"}</Text>
            </Text>
            <Text style={styles.normal}>
              This is page one
            </Text>
            <Button
              title="This is a button to two"
              onPress={() => this.setState({show: 'pageTwo' })}
            />
          </ScrollView>
        ) : null}
        {this.state.show === 'pageTwo' ? (
          <ScrollView style={styles.background}>
            <Text style={styles.header}>
              <Text>{"Page two"}</Text>
            </Text>
            <Text style={styles.normal}>
              This is page two
            </Text>
            <Button
              title="This is a button to one"
              onPress={() => this.setState({show: 'pageOne' })}
            />
          </ScrollView>
        ) : null}
      </View>
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在 React Native 中更新我的 State 时,我的 State 发生了变化,但组件消失了 如何解决这个问题? - My State Does Change But Components Are Disappear When I Update My State In React Native How To Fıx This? 如何在React中的状态下更改数组? - How can I change an array in a state in React? 如何在React Native中更改按钮的颜色而不重置状态? - How can I change the colour of a button in react native without reseting state? react native:当我从另一个页面单击按钮时,如何更改视图? - react native: How can I change a view when i click a button from another page? 如何使用React Native修改状态Text? - How can I modify the state Text with React Native? 我如何在本机反应中更新 state object? - How can i update state object in react native? 如何初始化我的反应本机状态? - How to init my react-native state? 登录前后如何在React Native中更改我的抽屉导航器的选项 - How can i change the options of my drawerNavigator in React Native before and after loged in 如何映射对象数组,删除一个对象并将我的状态设置为 react-native 中的新数组? - How can I map through an array of objects, remove one and set my state as the new array in react-native? 如何获取我的Firebase侦听器以在React Native应用程序中将数据加载到Redux状态,以便可以在ComponentDidMount函数中读取数据? - How can I get my firebase listener to load data to my redux state in a React Native app so I can read the data within my ComponentDidMount function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM