Skip to content

Passing dynamic data to created layout. #458

Answered by ankri
taine-zuse asked this question in Q&A
Discussion options

You must be logged in to vote

I would suggest putting the data into a Context.

And in your component you do:

const data = useData();
// data is either null or the data you need
// data is always null in editor mode (<Editor enabled />) 
// data can be null in (<Editor enabled={false} />) or the data you need

Pseudo code for useData and the Provider

import * as React from 'react';
import { useEditor } from '@craftjs/core';
// or react-query, your own fetch, etc.
import useSWR from 'swr';

const DataContext = React.createContext(null);

// pseudo code
export const useData = () => {
  const { enabled } = useEditor((state) => ({ enabled: state.options.enabled }));
  const data = React.useContext(DataContext);

  if (!enabled

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@ankri
Comment options

@taine-zuse
Comment options

Answer selected by taine-zuse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants