Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom React Native components with child nodes

I want to create a React Native component in pure JavaScript, composed of other components such as TouchableOpacity and Text. I have a couple of buttons in my app that are composed of that two components so I thought it would be nice to learn how to create my own components for better code reuse.

The finished component should look more or less like this:

<Button>
  Tap me!
</Button>

And this is the code I made for the component so far:

class Button extends Component {
  render () {
    <TouchableOpacity style={styles.button}>
      <Text style={styles.textButton}>
      </Text>
    </TouchableOpacity>
  }
};

However, I don't know how I can use the Tap me! inner child text in my component and I don't really get how I can make my component to accept custom props and the TouchableOpacity and Text props.

PS: I know there are some React Native components like this out there, but I prefer to create my own in order to learn how I can build this kind of custom components. Also, React Native is awesome but I cannot find how to build things like this in their docs and I think it's a really interesting exercise for people starting in React.

like image 418
amb Avatar asked Aug 28 '15 07:08

amb


People also ask

How do you create a component with kids React?

TLDR: You can create a reusable Layout component in React without knowing its nested components by making use of React's “children” prop. By passing in the children prop to your Layout component, you can add JSX of { children } to your component which will inject any nested components into Layout when you call it.

Can React components have children?

In React, a component can have one, many, or no children.

How do I add a child to a component?

Conclusion. We can pass in children components, strings or expressions with JSX by putting them in between the opening and closing tags of a component and then access them in the component via props. children . We can pass in any expression, function, or components in between the tags.

Can we pass props from child to parent component?

You can't pass props from child to parent in React, it's only one way (from parent to child). You should either: Put the state in the parent component and manipulate it from the child component by passing the setter function in the props.


2 Answers

You can access the inner text via this.props.children and you can pass properties either manually (via this.props) or using ... operator. More of this is described in react.js documentation (note - not React Native docs!). The most relevant parts of the documentation are:

  • http://facebook.github.io/react/docs/multiple-components.html
  • https://facebook.github.io/react/docs/components-and-props.html

It's general approach of React Native documentation that rather than describing all react concepts, they described only the React Native parts and the actual concept is described in the web/original version of React.

like image 187
Jarek Potiuk Avatar answered Oct 03 '22 13:10

Jarek Potiuk


you can checkout this repo from github: https://github.com/future-challenger/react-native-tabs

some code here:

<View style={[styles.tabbarView, this.props.style, this.state.keyboardUp && styles.hidden]}>
    {React.Children.map(this.props.children.filter(c=>c),(el)=>
    <TouchableOpacity key={el.props.name + "touch"}
        testID={el.props.testID}
        style={[styles.iconView, this.props.iconStyle, (el.props.name || el.key) == selected ? this.props.selectedIconStyle || el.props.selectedIconStyle || {} : {} ]}
        onPress={()=>!self.props.locked && self.onSelect(el)}
        onLongPress={()=>self.onSelect(el)}
        activeOpacity={el.props.pressOpacity}>
            {selected == (el.props.name || el.key) ? React.cloneElement(el, {selected: true, style: [el.props.style, this.props.selectedStyle, el.props.selectedStyle]}) : el}
    </TouchableOpacity>
)}

React.Children.map(this.props.children.filter...) is the key to deal with children components.

like image 32
Bruce Lee Avatar answered Oct 03 '22 15:10

Bruce Lee