Skip to content

MarvelSQ/use-properties-hook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

use-properties-hook

bind variables to current Component, like class-properties

install

npm install @marvelsq/use-properties-hook

use

  • useProperty(any):any bind single variable
  • useProperties(...any):[...any] bind multiple variable
import React, { useState } from 'react';
import { useProperties } from '@marvelsq/use-properties-hook';

export default function App({ data, updateData }) {
  // state
  const [active, setActive] = useState(null);
  // functions always same
  const [handleClick, handleRemove] = useProperties(
    index => setActive(index),
    targetIndex => {
      updateData(data.filter((data, index) => targetIndex !== index));
      setActive(data[index - 1] ? index - 1 : null);
    }
  );

  return <Table data={data} onClick={handleClick} onRemove={handleRemove} />;
}

inspired by Making setInterval Declarative with React Hooks

About

A React hook - instance variables with useRef

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published