3 April 2020

Alert Dialog example in react native

  • Create Components folder under src folder
  • Add AlertDialogEXP1.js file and add below Code
import React from "react";
import { StyleSheet, View, Button } from "react-native";

const AlertDialogEXP1 = () => {
  const ShowAlertDialog = () => {
    alert("Alert Dialog Title", "Alert Dialog Message", [
      {
        text: "Ask me later",
        onPress: () => console.log("Ask me later Button Clicked")
      },
      {
        text: "Cancel",
        onPress: () => console.log("Cancel Button Pressed"),
        style: "cancel"
      },
      { text: "OK", onPress: () => console.log("OK ButtonPressed") }
    ]);
  };
  return (
    <View style={styles.MainContainer}>
      <Button title="Show Alert Dialog " onPress={ShowAlertDialog} />
    </View>
  );
};
export default AlertDialogEXP1;

const styles = StyleSheet.create({
  MainContainer: {
    justifyContent: "center",
    flex: 1,
    margin: 10
  }
});
  • Open App.js file and update code as below\
import React from 'react';
import { View,StyleSheet } from 'react-native';
import AlertDialogEXP1 from './src/components/AlertDialogEXP1'

 const  App=()=> {
  return (
    <View style={styles.container}>
      <AlertDialogEXP1/>
    </View>
  );
}
export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    paddingTop:80
  },
});

Output:

2 April 2020

Activity Indicator example in react native

  • Create Components folder under src folder
  • Add ActivityIndicatorEXP1.js file and add below Code
import React from "react";
import {
Platform,
StyleSheet,
View,
ActivityIndicator,
Text
} from "react-native";

const ActivityIndicatorEXP1 = () => {
return (
<>
<Text>ActivityIndicator example</Text>
<View style={styles.MainContainer}>
<ActivityIndicator size="large" color="#F44336" />
<ActivityIndicator size="small" color="#E91E63" />
<ActivityIndicator size="large" color="#00BCD4" />
<ActivityIndicator size="small" color="#009688" />
<ActivityIndicator size="large" color="#4CAF50" />
<ActivityIndicator size="small" color="#FF9800" />
</View>
</>
);
};
export default ActivityIndicatorEXP1;
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
paddingTop: Platform.OS === "ios" ? 20 : 0,
flexDirection: "row",
justifyContent: "space-around"
}
});
  • Open App.js file and update code as below
import React from "react";
import { View } from "react-native";
import ActivityIndicatorEXP1 from "../src/components/ActivityIndicatorEXP1";

const App = () => {
return (
<View>
<ActivityIndicatorEXP1 />
</View>
);
};
export default App;

Output:




Accordion example in react native

  • Create Components folder under src folder
  • Add AccordionEXP1.js file and add below Code
import React, { Component } from "react";

import {
Platform,
LayoutAnimation,
StyleSheet,
View,
Text,
ScrollView,
UIManager,
TouchableOpacity
} from "react-native";

class AccordionEXP1 extends Component {
constructor() {
super();

this.state = {
updated_Height: 0
};
}

componentWillReceiveProps(update_Props) {
if (update_Props.item.expanded) {
this.setState(() => {
return {
updated_Height: null
};
});
} else {
this.setState(() => {
return {
updated_Height: 0
};
});
}
}

shouldComponentUpdate(update_Props, nextState) {
if (update_Props.item.expanded !== this.props.item.expanded) {
return true;
}

return false;
}

render() {
return (
<View style={styles.Panel_Holder}>
<TouchableOpacity
activeOpacity={0.7}
onPress={this.props.onClickFunction}
style={styles.Btn}
>
<Text style={styles.Panel_Button_Text}>{this.props.item.title} </Text>
</TouchableOpacity>

<View style={{ height: this.state.updated_Height, overflow: "hidden" }}>
<Text style={styles.Panel_text}>{this.props.item.body}</Text>
</View>
</View>
);
}
}

export default class App extends Component {
constructor() {
super();

if (Platform.OS === "android") {
UIManager.setLayoutAnimationEnabledExperimental(true);
}

const array = [
{
expanded: false,
title: "Panel 1",
body: " this is the Animated Accordion Panel."
},
{
expanded: false,
title: "Panel 2",
body: " this is the Animated Accordion Panel."
},
{
expanded: false,
title: "Panel 3",
body: " this is the Animated Accordion Panel."
},
{
expanded: false,
title: "Panel 4",
body: " this is the Animated Accordion Panel."
},
{
expanded: false,
title: "Panel 5",
body: " this is the Animated Accordion Panel."
},
{
expanded: false,
title: "Panel 6",
body: " this is the Animated Accordion Panel."
},
{
expanded: false,
title: "Panel 7",
body: " this is the Animated Accordion Panel."
},
{
expanded: false,
title: "Panel 8",
body: " this is the Animated Accordion Panel."
},
{
expanded: false,
title: "Panel 9",
body: " this is the Animated Accordion Panel."
},
{
expanded: false,
title: "Panel 10",
body: " this is the Animated Accordion Panel."
}
];

this.state = { AccordionData: [...array] };
}

update_Layout = index => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);

const array = this.state.AccordionData.map(item => {
const newItem = Object.assign({}, item);

newItem.expanded = false;

return newItem;
});

array[index].expanded = true;

this.setState(() => {
return {
AccordionData: array
};
});
};

render() {
return (
<View style={styles.MainContainer}>
<ScrollView
contentContainerStyle={{ paddingHorizontal: 10, paddingVertical: 5 }}
>
{this.state.AccordionData.map((item, key) => (
<AccordionEXP1
key={key}
onClickFunction={this.update_Layout.bind(this, key)}
item={item}
/>
))}
</ScrollView>
</View>
);
}
}

const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: "center",
paddingTop: Platform.OS === "ios" ? 20 : 0
},

Panel_text: {
fontSize: 18,
color: "#000",
padding: 10
},

Panel_Button_Text: {
textAlign: "center",
color: "#fff",
fontSize: 21
},

Panel_Holder: {
borderWidth: 1,
borderColor: "#FF6F00",
marginVertical: 5
},

Btn: {
padding: 10,
backgroundColor: "#FF6F00"
}
});
  • Open App.js file and update code as below\
import React from "react";
import { View } from "react-native";
import AccordionEXP1 from "../src/components/AccordionEXP1";

const App = () => {
return (
<View>
<AccordionEXP1 />
</View>
);
};
export default App;

Output:

Detect .net Code Quality and Security issues


Ternary Operator Example in react js

  • Create Components folder under src folder
  • Add TernaryOperatorEXP1.js file and add below Code
import React, { useState } from "react";

const TernaryOperatorEXP1 = () => {
const [isLoggedIn, setIsLoggedIn] = useState(true);

return (
<div>
<h3>Ternary Operator Example </h3>
{isLoggedIn ? <div> Welcome User</div> : <div> You need to login</div>}
</div>
);
};

export default TernaryOperatorEXP1;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import TernaryOperatorEXP1 from "./components/TernaryOperatorEXP1";

function App() {
return (
<div className="App">
<TernaryOperatorEXP1 />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:



State From Parent To Child Class & Child Function in react js

  • Create Components folder under src folder
  • Add StateFromParentToChild.js file and add below Code
import React, { Component } from "react";

class StateFromParentToChild extends Component {
constructor() {
super();
this.state = {
name: "Adi"
};
}
render() {
return (
<div>
State From Parent To Child Class & Child Function
<div>Parent Class</div>
<JTP name={this.state.name} />
<PopsInFunEXP1 name={this.state.name} />
</div>
);
}
}
class JTP extends Component {
render() {
return (
<div>
<div>Child Class</div>
<div>Welcome to {this.props.name}</div>
</div>
);
}
}
function PopsInFunEXP1(props) {
return (
<div>
<div>Child Funtion</div>
<div>{props.name}</div>
</div>
);
}
export default StateFromParentToChild;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import StateFromParentToChild from "./components/StateFromParentToChild";

function App() {
return (
<div className="App">
<StateFromParentToChild />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:



Short Circuit Operator example in react js

  • Create Components folder under src folder
  • Add ShortCircuitOperatorEXP1.js file and add below Code
import React, { useState } from "react";

const ShortCircuitOperatorEXP1 = () => {
const [isLoggedIn, setIsLoggedIn] = useState(true);

return (
<div>
<h3>Short Circuit Operator Example </h3>
{isLoggedIn && <div> Welcome User</div>}
</div>
);
};

export default ShortCircuitOperatorEXP1;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import ShortCircuitOperatorEXP1 from "./components/ShortCircuitOperatorEXP1";

function App() {
return (
<div className="App">
<ShortCircuitOperatorEXP1 />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output: