react use callback code example

Example 1: use propTypes in react function

import React from 'react';
import { PropTypes } from 'prop-types';

const student = (props) => {
  return (
    

Student Name: {props.name}

Age: {props.age}

); }; student.propTypes = { name: PropTypes.string, age: PropTypes.number }; export default student;

Example 2: useRef

/*
	A common use case is to access a child imperatively: 
*/

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
      
      
    
  );
}

Example 3: callback in react

import React, { useCallback } from 'react';

function MyComponent() {
  // handleClick is the same function object
  const handleClick = useCallback(() => {    console.log('Clicked!');  }, []);
  // ...
}

Example 4: how to use hooks react

const App = () => {
const [students , setStudents] = useState([]);
  
  return (
// put in the jsx code here
  )
}

Tags:

Misc Example