1

This is my accord component action:

accord.js

const toggle = useCallback(() => {
  if (!disabled) {
    setExpand((expand) => !expand);
  }
}, [disabled, setExpand]);

<div className={`Accord`} onClick={toggle} >

How to invoke accord action from other components?

2 Answers 2

1

I suggest using custom hooks:

useAccord.js

const useAccord = (disabled) => {
  const [expand, setExpand] = useState(false);

  const toggle = useCallback(() => {
    if (!disabled) {
      setExpand((expand) => !expand);
    }
  }, [disabled, setExpand]);

  return { expand, toggle };
};

OtherComponent.js

const OtherComponent = () => {
  const { expand, toggle } = useAccord(false);

  return <div className={`Accord`} onClick={toggle}></div>;
};

Ref: https://reactjs.org/docs/hooks-custom.html

Sign up to request clarification or add additional context in comments.

Comments

0

generally you can declare a function in parent component and pass it as a props to child component. now if you invoke it in child component, function execute in parent.

this concept can be developed with props, context, redux

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.