0

I am trying to send value of TextInput to another Class Function in console.log. My approach is when the button is pressed the value FromStr in TextInput will got passed into another class function. Here's my code

import React, { Component } from "react";
import { StyleSheet, Text, View, TextInput, Button } from "react-native";
import receiveMessage from "./receiveMessage"

export default class WeatherProject extends Component{

  constructor (props) {
    super(props);

    this.state={
        From:'',
        FromStr:'',
    }
  }

 changeText=(From)=>{
  this.setState({From})
 }
 changeText1=(To)=>{
  this.setState({To})
 }
 onPress = ()=>{
  this.setState({FromStr: this.state.From})
  receiveMessage.receiveMessage();
 }

render(){
return (
<View>
    <View style={styles.inputFields}>
    <TextInput placeholder="From" id="from" style={styles.fromField} onChangeText={this.changeText} />
  <View style={styles.buttonStyle}>
  <Button
      title={"Go Back"}
      color="#f194ff"
      onPress={this.onPress}
  ></Button>
 </View>
    </View>
      </View>

);
}
}

receiveMessage.js

import React, { Component } from "react";
export default class receiveMessage extends Component {

        static receiveMessage=()=>{
        console.log(this.state.FromStr)
      }
  }
3
  • 1
    receiveMessage isn't a valid react component name, it should be PascalCased. What is the relationship between WeatherProject and ReceiveMessage? Parent-child? Siblings? In react data typically flows in one direction, down the tree from parent to children. Callbacks allow data to passed back up the tree though. Commented Jan 29, 2020 at 7:30
  • If you are working in React Native try passing the data to another component by using props. check out great tutorial here : reactjs.org/docs/components-and-props.html Commented Jan 29, 2020 at 9:00
  • @RaviSingh i cant compare it with my code. Can you help me in this solution Commented Feb 1, 2020 at 9:59

2 Answers 2

1

React does not allow to pass the data between react components in this way.

Following is way to pass the data between components in React. To get more insights please follow

import React, { Component } from 'react';

class WeatherProject extends Component {
   render() {
    const messageToPassed = 'Hello';
    return (
      <div>
        <ReceiveMessage message={messageToPassed} />
     </div>
    );
   }
}

const ReceiveMessage = props => <h1>{props.message}</h1>;
export default App;
Sign up to request clarification or add additional context in comments.

Comments

0

here we pass the value from sidemenu component by raising an event

App.js

class App extends React.Component {
handleSubmit = (e, data) => console.log(`my data from props`, data);
render() {
    return (
            <Sidemenu 
              onSubmit={(e, data)=>this.handleSubmit(e, data)} />
      );
  }
}

SideMenu.js

const Sidemenu = props => {
const { onSubmit} = props;
    return (
            <button onClick={(e, type)=>this.onSubmit(e, 'mydata')} />
      );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.