[英]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.