Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to pass state from one component to another in functional component

//here i am creating a simple count state to show you the passing of the state:
import React, { useState } from "react";
import ComponentOne from "./components/ComponentOne/ComponentOne";

function App() {
	const [count, setCount] = useState(0);
    const increaseCount = () => {
    	const newCount = count + 1; 
      	setCount(newCount)
    };
  return (
  	<div>
    	<h2>Count: {count} </h2>
		<button onClick = {increaseCount}>Increase Count</button>
		
		//ComponentOne and passing state through props: 
		<ComponentOne count={count}></ComponentOne>
    </div> 
  )
}


//Above you can see that there is a state and count function that will increase 
//the current value of the count trough setCount methot. Now I will pass this 
//another component. 

Const ComponentOne = (porps) => {
	return (
      <div> 
        //rechieving the state of count through props.count cause we 
        // have given the props in this componenet: 
        <h3>the count state which is passed through props: {props.count} </h3>
      </div> 
    );
}


export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: babel start command nodejs 
Javascript :: make random letter capital in string javascript 
Javascript :: replace in javascript 
Javascript :: run function on page resize javascript 
Javascript :: split string into two parts javascript 
Javascript :: javascipt delay 
Javascript :: get in redis 
Javascript :: is digit javascript 
Javascript :: localstorage in js 
Javascript :: javascript The replace() method 
Javascript :: node js vs jquery 
Javascript :: add decimals javascript 
Javascript :: object methods in javascript 
Javascript :: what does getattribute return null for data-* attributes 
Javascript :: @apify/http-request 
Javascript :: save jshint 
Javascript :: array concat in javascript 
Javascript :: react alice carousel 
Javascript :: radio group react 
Javascript :: yarn add next auth 
Javascript :: latitude longitude to km javascript 
Javascript :: js find duplicates in array 
Javascript :: uppercase first letter javascript 
Javascript :: html to pdf nodejs 
Javascript :: remove everything from mongodb databaase mongoose 
Javascript :: javascript connect metamask 
Javascript :: javascript if not 
Javascript :: datepicker min max date 
Javascript :: how to get keys from request headers in express 
Javascript :: find an object from array of objects javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =