craft.js icon indicating copy to clipboard operation
craft.js copied to clipboard

How to make connections two components

Open xushichen opened this issue 1 year ago • 3 comments

I have two component: A, B How to let the B component know the props update of the A component

xushichen avatar Dec 21 '23 03:12 xushichen

you can subscribe the B component to the props update of A component:

function BComponent() {
  const { store } = useEditor()
   
  useEffect(() => {
    const unsubscribe = store.subscribe(
      state => {
        return {
                // you have to get the id of A component
	        props: state.nodes[idOfAComponent].data.props
        }
      },
      collected => {
         console.log(collected.props);
      }
    )
    return () => unsubscribe();
  }, [store])
  
  // ...
}

rohailtaha avatar Dec 21 '23 09:12 rohailtaha

How to get A component id?can i set the id?

xushichen avatar Dec 21 '23 10:12 xushichen

How to get A component id?can i set the id?

You can store the A component's id inside the B components' props or custom object considering the A component has already been created when you are creating the B component.

rohailtaha avatar Dec 21 '23 17:12 rohailtaha