反应原生)如何将状态传递给另一个文件?

时间:2020-05-18 08:37:30

标签: react-native

我问你一个问题,因为在使用React Native开发应用程序时遇到问题。

我想在多个阶段输入“ Add.js”生成的“状态”所需的信息。

最后,将在每个步骤中输入的信息应用于“状态”,以显示在名为“ info.js”的文件中。

我正在编写一个包含以下课程的程序。

  1. 在“ Add.js”中创建“状态”,输入“名称,剂量”
  2. 在“ Reminder.js”的“循环,特殊”中输入“布尔类型”
  3. ...其他输入步骤...
  4. 发布在“ info.js”中输入的“状态”信息

我很好奇的是从1级到2级的过程。

我想将“状态”移交给“ Reminder.js”,包括在“ Add.js”中输入的“名称,目的地”信息。我想在“ Reminder.js”中输入其他信息。 我应该如何修改此代码?

Add.js

export default class Add extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            name: "",
            dosage: 0,
            Cycle: false,
            Specific: false,
        }
    }
    static propTypes = {
        name: PropTypes.string.isRequired,
        dosage: PropTypes.number.isRequired,      
    }
    render() {
        const {name, dosage}=this.state
        return (
            <View>
                <Title>Add Medication</Title>
                <SubTitle>Step 1. Medication Information</SubTitle>
                <Descript>1. What is the name of the medication?</Descript>
                <TextBox
                    placeholder="Name"
                    style={{ marginBottom: 30 }}
                    onChangeText={(name)=>this.setState({name})}
                    value={this.state.name}
                />
                <Descript>2. What is the dosage?(Optional)</Descript>
                <TextBox
                    placeholder="e.g. 2"
                    onChangeText={(dosage)=>this.setState({dosage})}
                    value={this.state.dosage}
                />
                <Condition>Should be numeric</Condition>
                <Button title="Next" onPress={() => this.props.navigation.navigate("Reminder")} />
            </View>
        );
    }
}

Add.js ==(state)==> Reminder.js

Reminder.js

export default ({navigation}) => (
    <View>
        <Title>Add Medication</Title>
        <SubTitle>Step 2. Step Reminder</SubTitle>
        <Descript>How do you want to set the reminder?</Descript>
        <Button onPress={()=> navigation.navigate("Specific")}>
            <ButtonText>Set specific time</ButtonText>
        </Button>
        <Button onPress={()=> navigation.navigate("Cycle")}>
            <ButtonText>Set cycle</ButtonText>
        </Button>
    </View>
);

0 个答案:

没有答案