Answers for "react custom hooks"

11

react custom hooks

import React, { useState } from "react";
 
// custom hooks useForm
const useForm = callback => {
  const [values, setValues] = useState({});
  return {
    values,
    onChange: e => {
      setValues({
        ...values,
        [e.target.name]: e.target.value
      });
    },
    onSubmit: e => {
      e.preventDefault();
      callback();
    }
  };
};
 
// app component
export default function App() {
  const { values, onChange, onSubmit } = useForm(() => {
    console.log(values.username);
    console.log(values.email);
  });
  return (
    <div>
      <form onSubmit={onSubmit}>
        <input type="text" name="username" onChange={onChange} />
        <input type="email" name="email" onChange={onChange} />
        <input type="submit" value="Sing-in" />
      </form>
    </div>
  );
}
Posted by: Guest on July-26-2020
8

react useEffect

import React, { useEffect } from 'react';

export const App: React.FC = () => {
  
  useEffect(() => {
        
  }, [/*Here can enter some value to call again the content inside useEffect*/])
  
  return (
    <div>Use Effect!</div>
  );
}
Posted by: Guest on May-29-2020
7

react useEffect

import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';

function LifecycleDemo() {
  // It takes a function
  useEffect(() => {
    // This gets called after every render, by default
    // (the first one, and every one after that)
    console.log('render!');

    // If you want to implement componentWillUnmount,
    // return a function from here, and React will call
    // it prior to unmounting.
    return () => console.log('unmounting...');
  }, [ // dependencies to watch = leave blank to run once or you will get a stack overflow  ]);

  return "I'm a lifecycle demo";
}

function App() {
  // Set up a piece of state, just so that we have
  // a way to trigger a re-render.
  const [random, setRandom] = useState(Math.random());

  // Set up another piece of state to keep track of
  // whether the LifecycleDemo is shown or hidden
  const [mounted, setMounted] = useState(true);

  // This function will change the random number,
  // and trigger a re-render (in the console,
  // you'll see a "render!" from LifecycleDemo)
  const reRender = () => setRandom(Math.random());

  // This function will unmount and re-mount the
  // LifecycleDemo, so you can see its cleanup function
  // being called.
  const toggle = () => setMounted(!mounted);

  return (
    <>
      <button onClick={reRender}>Re-render</button>
      <button onClick={toggle}>Show/Hide LifecycleDemo</button>
      {mounted && <LifecycleDemo/>}
    </>
  );
}

ReactDOM.render(<App/>, document.querySelector('#root'));
Posted by: Guest on April-11-2020
5

hooks in react

import React, { useState, useEffect } from "react";
export default props => {
  console.log("componentWillMount");
  console.log("componentWillReceiveProps", props);
  const [x, setX] = useState(0);
  const [y, setY] = useState(0);
  const [moveCount, setMoveCount] = useState(0);
  const [cross, setCross] = useState(0);
  const mouseMoveHandler = event => {
    setX(event.clientX);
    setY(event.clientY);
  };
  useEffect(() => {
    console.log("componentDidMount");
    document.addEventListener("mousemove", mouseMoveHandler);
    return () => {
      console.log("componentWillUnmount");
      document.removeEventListener("mousemove", mouseMoveHandler);
    };
  }, []); // empty-array means don't watch for any updates
  useEffect(
    () => {
      // if (componentDidUpdate & (x or y changed))
      setMoveCount(moveCount + 1);
    },
    [x, y]
  );
useEffect(() => {
    // if componentDidUpdate or componentDidMount
    if (x === y) {
      setCross(x);
    }
  });
  return (
    <div>
      <p style={{ color: props.color }}>
        Your mouse is at {x}, {y} position.
      </p>
      <p>Your mouse has moved {moveCount} times</p>
      <p>
        X and Y positions were last equal at {cross}, {cross}
      </p>
    </div>
  );
};
Posted by: Guest on May-26-2020
0

react hooks, react custom hooks

//Go to https://www.30secondsofcode.org/react/ for most commonly 
//used custom hooks and components
Posted by: Guest on September-14-2021
0

react custom hooks

// custom useMethods Hooks
export const useMethod = (props) => {
	const keys = Object.keys(props.methods)
	const methods = {}
	for (let i in keys) {
		Object.defineProperties(methods, {
			[keys[i]]: {
				value: props.methods[keys[i]],
				writable: true,
				enumerable: true
			}
		})
	}
	return methods
}

// call custom hooks useMethod like this
const handler = useMethod({
  methods: {
    increment() {
      setIncrement(increment + 1)
    },
    decrement() {
      setDecrement(decrement - 1)
    }
  }
})

// call custom hooks userMethod with useCallback
const [increment, setIncrement] = useState(0)
const [decrement, setDecrement] = useState(0)

const handler = useMethod({
  methods: {
    increment: useCallback(() => setIncrement(increment + 1), [increment]),
    decrement: useCallback(() => setDecrement(decrement - 1), [decrement])
  }
})
Posted by: Guest on September-25-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language