0

Here i am using two components,

var first = React.createClass(){
     render:function(){
          <View>
             <Text>First Component</Text>
             <TextInput style={{borderWidth:1,borderColor:'red'}} onPress={getData()}/>
          </View>
      }
})
    function getData(){
      <second/>
    }
    var second = React.createClass(){
         render:function(){
              <View>
                 <ScrollView>
                   <Text>Second component</Text>
                 </ScrollView>
              </View>
          }
    })

Required Result: First Component Second component

1 Answer 1

1

it looks like you're trying to show a new component when something is clicked? The way I would handle this is by using state. Generally you only want JSX to show up in your render method.

So what you should do is have something like:

var first = React.createClass({
  getInitialState: function() {
    return {
      showSecond: false
    };
  },
  _handlePress: function() {
    return this.setState(showSecond(true));
  },
  render: function() {
    <View>
      <View style={styles.first}>
        <Text> ... </Text>
        <TextInput />
        <TouchableHighlight onPress={@_handlePress}>
          <Text> Button </Text>
        </TouchableHighlight>
      </View>
      {if showSecond
        <View style={styles.second}>
          <ScrollView>
            <Text> Scroll Item </Text>
          </ScrollView>
        </View>
      }
    </View>
  }
});

Now what happens is when you press the button you change state, which rerenders your component, and now that the state is true, it shows the second component

Sign up to request clarification or add additional context in comments.

4 Comments

No i want to call the second component from external function only, that's the scenario i need
so i'm not sure why you would HAVE to use an external function, but even so, you would have to do a similar thing as above. You have to specify where in your rendered DOM the new component has to show up by using the if statement and watching for state. Then instead of having the component already there like above, you could call a function that returns the component. So on press, change state. The new state triggers the if statement and in turn the external function that returns JSX. It seems highly unnecessary to do it that way though.
The second component I am talking about is required to be called in several other components. To enable code reuse and ease of maintenance I wanted to write a set of components that will be called within other components. This is how we are doing it in react. Will try in react-native and revert. Thanks for your help.
So if you have a component that shows up a lot, like a spinner. I would just write it in its own file with module.exports = React.createClass({..}) and then in every component that needs it, just require('./ui/spinner') to add the spinner to wherever you need it. That's how I do it for React and React-Native

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.