5 April 2020

Live Clock in react js

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

class LiveClockUpdate extends Component {
constructor(props) {
super(props);
this.state = { date: new Date() };
}
componentDidMount() {
this.timerID = setInterval(() => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this.timerID);
}

tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h2>{this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
export default LiveClockUpdate;
  • Open App.js file and update code as below
import React from "react";
import ReactDOM from "react-dom";
import LiveClockUpdate from "./components/LiveClockUpdate";

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

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

Output:


4 April 2020

Creating a Model for an Existing Database in Entity Framework Core


  • Create or Open Project in Visual Studio.
  • Then in Visual Studio go to Tools Menu -> NuGet Package Manger -> Package Manger Console -> then use below command to create a model


PM> Scaffold-DbContext "Server=Database Server Name; Database=Database Name; Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models


Note: In above Command update below details based on you database name and requirement
  • Database Server Name: Your Database Server Name
  • Database Name: Your Database Name
  • Trusted_Connection=True: If you are using windows authentication. If you want to use SQL Authentication then add your login details in place of  "Trusted_Connection=True".
  • -OutputDir Models: The -OutputDir parameter specifies the directory where we want to generate all the classes which is the Models folder in this case.


3 April 2020

View example in react native

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

const ViewEXP1 = () => {
  return (
    <View>
      <Text>View example</Text>
      <Text>Line 1</Text>
      <Text>Line 2</Text>
      <Text>Line 2</Text>
    </View>
  );
};
export default ViewEXP1;
  • Open App.js file and update code as below
import React from 'react';
import { View,StyleSheet } from 'react-native';
import ViewEXP1 from './src/components/ViewEXP1'

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

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

State example in react native

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

class stateEXP extends React.Component {
  state = {
    myState: "Adi"
  };
  render() {
    return (
      <View>
        <Text>State example</Text>

        <Text> {this.state.myState}</Text>
      </View>
    );
  }
}
export default stateEXP;
  • Open App.js file and update code as below
import React from 'react';
import { View,StyleSheet } from 'react-native';
import stateEXP from './src/components/stateEXP'

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

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



Single Line And Multiple Line Comments Syntax

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

const SingleAndMultipleLineComments = () => {
  return (
    <View>
      <Text>Single Line And Multiple Line Comments Syntax</Text>
      <Text>Single Line Comments</Text>
      {/* <Text>Line 1</Text> */}
      <Text>Line 2</Text>
      <Text>Line 3</Text>
      <Text>Line 4</Text>
      <Text>Line 5</Text>
      <Text>Multiple Line Comments</Text>
      {/* <Text>Line 6</Text>
      <Text>Line 7</Text>
      <Text>Line 8</Text> */}
    </View>
  );
};
export default SingleAndMultipleLineComments;


React passing parameter via onclick event using Arrow function

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

const onClickEvent = id => {
  alert(id);
};

const PassingParameterViaOnclick2 = () => {
  return (
    <View>
      <Text>
        React passing parameter via onclick event using Arrow function
      </Text>
      <Button
        title="Submit"
        value="buttonValue"
        onPress={() => {
          onClickEvent(10);
        }}
      />
    </View>
  );
};

export default PassingParameterViaOnclick2;
  • Open App.js file and update code as below\
import React from 'react';
import { View,StyleSheet } from 'react-native';
import PassingParameterViaOnclick2 from './src/components/PassingParameterViaOnclick2'

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

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

Output:

React passing parameter via onclick event using Bind

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

const onClickEvent = message => {
  alert(message);
};

const PassingParameterViaOnclick1 = () => {
  return (
    <View>
      <Text>React passing parameter via onclick event using Bind </Text>
      <Button
        title="Submit"
        value="buttonValue"
        onPress={onClickEvent.bind(this, "hiiii")}
      />
    </View>
  );
};

export default PassingParameterViaOnclick1;
  • Open App.js file and update code as below\
import React from 'react';
import { View,StyleSheet } from 'react-native';
import PassingParameterViaOnclick1 from './src/components/PassingParameterViaOnclick1'

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

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

Output:

Passing parameter via onclick event in React Native

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

const onClickEvent = (id, name) => e => {
  alert(id);
  alert(name);
};

const PassingParameterViaOnclick = () => {
  return (
    <View>
      <Text>React passing parameter via onclick event </Text>
      <Button
        title="Submit"
        value="buttonValue"
        onPress={onClickEvent(10, "Adi")}
      />
    </View>
  );
};

export default PassingParameterViaOnclick;
  • Open App.js file and update code as below\
import React from 'react';
import { View,StyleSheet } from 'react-native';
import PassingParameterViaOnclick from './src/components/PassingParameterViaOnclick'

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

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

Output:

Grid format example in react native

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

const data = [
  { id: "a", value: "A" },
  { id: "b", value: "B" },
  { id: "c", value: "C" },
  { id: "d", value: "D" },
  { id: "e", value: "E" },
  { id: "f", value: "F" }
];
const numColumns = 3;
const size = Dimensions.get("window").width / numColumns;
console.log(size);
const styles = StyleSheet.create({
  container: {
    margin: 10
  },
  itemContainer: {
    width: size,
    height: size
  },
  item: {
    flex: 1,
    margin: 3,
    backgroundColor: "lightblue"
  }
});

function Grid() {
  return (
    <View style={styles.container}>
      <FlatList
        data={data}
        renderItem={({ item }) => (
          <View style={styles.itemContainer}>
            <Text style={styles.item}>{item.value}</Text>
          </View>
        )}
        keyExtractor={item => item.id}
        numColumns={numColumns}
      />
    </View>
  );
}
export default Grid;
  • Open App.js file and update code as below\
import React from 'react';
import { View,StyleSheet } from 'react-native';
import Grid from './src/components/Grid'

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

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

Output:


Reusable button component react native example

  • Create Components folder under src folder
  • Add button Reusable component ButtonCOM.js file and add below Code
import React from "react";
import { Button } from "react-native";
const ButtonCOM = ({ title, decreaseCount }) => {
  return <Button onPress={decreaseCount} title={title} />;
};
export default ButtonCOM;
  • Use Reusable button component  in followed file ButtonEXP1.js file and add below Code
import React from "react";
import { Button, View, Text } from "react-native";

const callFun = () => {
  alert("Clicked on Button!!!");
};

const ButtonEXP1 = () => {
  return (
    <View>
      <Text> Reusable button component react native example</Text>
      <Button onPress={callFun} title="Test Button" color="#0000FF" />
    </View>
  );
};
export default ButtonEXP1;
  • Open App.js file and update code as below\
import React from 'react';
import { View,StyleSheet } from 'react-native';
import ButtonEXP1 from './src/components/ButtonEXP1'

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

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

Output: