React native 从另一个组件读取组件状态

React native 从另一个组件读取组件状态,react-native,React Native,我对本土发展的反应还很陌生。我做了一些研究,但没有达到我想要达到的目标。我的*.js文件有两个组件。一个是星级的组成部分。另一个包含基本视图。在视图中,我添加了一个按钮。我想做的是,当用户点击按钮时,会收集文本字段(alredy working)的输入和当前评级,以便通过电子邮件发送 问题: 1) 这只能通过实现通量模式/使用redux来实现吗? 2) 如果是:是否所有redux组件(操作、减缩器、存储)都必须放在单独的文件中? 3) 如果没有:怎么做 这是我目前的代码: import Reac

我对本土发展的反应还很陌生。我做了一些研究,但没有达到我想要达到的目标。我的*.js文件有两个组件。一个是星级的组成部分。另一个包含基本视图。在视图中,我添加了一个按钮。我想做的是,当用户点击按钮时,会收集文本字段(alredy working)的输入和当前评级,以便通过电子邮件发送

问题: 1) 这只能通过实现通量模式/使用redux来实现吗? 2) 如果是:是否所有redux组件(操作、减缩器、存储)都必须放在单独的文件中? 3) 如果没有:怎么做

这是我目前的代码:

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  TextInput,
  View
} from 'react-native';
import StarRating from 'react-native-star-rating';
import Button from 'react-native-button';
import {Actions} from 'react-native-router-flux';

export class GeneralStarExample extends Component {

  constructor(props) {
    super(props);
    this.state = {
      starCount: 4
    };
  }

  onStarRatingPress(rating) {
    this.setState({
      starCount: rating
    });
  }

  render() {
    return (
      <StarRating
        disabled={false}
        maxStars={5}
        rating={this.state.starCount}
        selectedStar={(rating) => this.onStarRatingPress(rating)}
        starColor={'gold'}
        emptyStarColor={'gold'}
      />
    );
  }
}

export default class FeedbackView  extends Component {

  constructor(props) {
    super(props);
    this.state = { text: 'Useless Placeholder' };
  }

  render () {
    return(

      <View style={styles.container}>
      <GeneralStarExample onUserInput={this.handleUserInput}/>
      <View style ={styles.textinput}>
        <TextInput
          style={{height: 240, width: 300, alignSelf: 'center', borderColor: 'gray', borderWidth: 1}}
          onChangeText={(text) => this.setState({text})}
          value={this.state.text}
          />
      </View>
      <Button onPress={() => Actions.refresh({text: this.state.text, rating: this.props.rating})}>Senden</Button>
      <Text>{this.props.text}</Text>
      <Text>{this.props.rating}</Text>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  }
});
import React,{Component}来自'React';
进口{
样式表,
文本,
文本输入,
看法
}从“反应本机”;
从“react native star rating”导入星号;
从“反应本机按钮”导入按钮;
从“react native router flux”导入{Actions};
导出类GeneralStarExample扩展组件{
建造师(道具){
超级(道具);
此.state={
星数:4
};
}
OnStartingPress(额定值){
这是我的国家({
星数:评级
});
}
render(){
返回(
此.onStartingPress(评级)}
starColor={'gold'}
emptyStarColor={'gold'}
/>
);
}
}
导出默认类FeedbackView扩展组件{
建造师(道具){
超级(道具);
this.state={text:'无用占位符'};
}
渲染(){
返回(
this.setState({text})}
值={this.state.text}
/>
Actions.refresh({text:this.state.text,rating:this.props.rating})}>Senden
{this.props.text}
{this.props.rating}
)
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
为内容辩护:“中心”,
对齐项目:“居中”,
背景颜色:“#F5FCFF”,
}
});
非常感谢, Defrian

1)您不一定需要实现Redux或任何类似Flux的体系结构,但在我看来,这是一种非常好的方法。Redux将帮助您保持代码的干净性和可伸缩性


2) 当然你可以把所有这些放在一个文件里。你的减速器和动作基本上就是函数。至于存储,您只需从redux模块调用
createStore()
,就可以创建它。

您不需要Flux/redux做任何事情,这是肯定的:)它们是完全可选的。