Advertisement
andresrm

NuevoMensajeComponent Final

Mar 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { StyleSheet, View, Button, TextInput } from 'react-native';
  3. import { setActualMessage, storeMessage } from '../../../actions/mensajes';
  4. import { connect } from 'react-redux';
  5. import { colors } from '../../../helpers/colors';
  6.  
  7. class NuevoMensajeComponent extends React.Component {
  8.     constructor(props) {
  9.         super(props);
  10.         this.color = colors[Math.floor(Math.random()*colors.length)];
  11.       }
  12.  
  13.     render(){
  14.         const { setActualMessage, addMessage, mensajeActual } = this.props;
  15.         return (
  16.             <View style={styles.container}>
  17.                 <View style={styles.messageBox}>
  18.                     <TextInput
  19.                         style={{ borderColor: 'gray', borderWidth: 1}}
  20.                         onChangeText={(text) => setActualMessage(text)}
  21.                         value={mensajeActual}
  22.                     />
  23.                 </View>
  24.  
  25.                 <View style={styles.button}>
  26.                     <Button
  27.                         onPress={ () => addMessage( {texto:mensajeActual, color:this.color}) }
  28.                         title="Send"
  29.                     />
  30.                 </View>
  31.             </View>
  32.         );
  33.     };
  34. };
  35.  
  36. const mapDispatchToProps = (dispatch) => {
  37.     return {
  38.         setActualMessage: texto => dispatch(setActualMessage(texto)),
  39.         addMessage: message => dispatch(storeMessage(message))
  40.     };
  41. };
  42.  
  43. const mapStateToProps = state => ({
  44.     mensajeActual: state.mensajeActual.texto
  45. });
  46.  
  47. const styles = StyleSheet.create({
  48.     container:{
  49.         flex:1,
  50.         flexDirection:'row',
  51.         justifyContent:'flex-start',
  52.         alignItems:'stretch',
  53.     },
  54.     messageBox:{
  55.         flex:1,
  56.         margin:5
  57.     },
  58.     button:{
  59.         marginRight:5
  60.     }
  61. });
  62.  
  63. export default connect(
  64.     mapStateToProps, mapDispatchToProps
  65.   )(NuevoMensajeComponent);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement