in React, React Native

Dynamically Hide/Show or Add/Remove component in react native

In react or react native the way component hide/show or add/remove does not work like in android or iOS. Most of us think there would be the similar stratedgy like

View.hide = true or parentView.addSubView(childView

But the way react native work is completely different. The only way to acheive this kind of functionality is to include your component in your DOM or remove from DOM.

 

Here in this example I am going set the visibility of text view based on the button click.


The idea behind this task is the create a state variable called stateĀ having the initial value set to falseĀ  when the button click event happens then it value toggles. Now we will use this state variable during the creation of component.

 

the only one thing to notice in this snippet is renderIf which is actually a function which will return the component passed to it based on the boolean value passed to it.
renderIf(predicate)(element).

renderif.js
'use strict';
const isFunction = input => typeof input === 'function';
export default predicate => elemOrThunk =>
predicate ? (isFunction(elemOrThunk) ? elemOrThunk() : elemOrThunk) : null;

Write a Comment

Comment