2
對於本機開發,我很新穎。我做了一些研究,但沒有得到我想要實現的工作。我的* .js文件有兩個組件。一個是星級評定的組件。另一個包含基本視圖。在視圖內我添加了一個按鈕。我想要發生的是當用戶點擊按鈕時,來自文本框(alredy working)的輸入和當前評分被收集以通過電子郵件發送。從另一個組件讀取組件狀態
問題: 1)這隻能通過實現flux模式/使用redux來實現嗎? 2)如果是:是否所有的redux組件(actions,reducer,store)都必須在不同的文件中吐出? 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',
}
});
非常感謝, Defrian
你不需要Flux/Redux來做任何事情 - 這是肯定的:)他們是完全可選的。 – EugZol