[RFC] Passing additional Properties in SliceZone with SliceProps
See original GitHub issueIs your feature request related to a problem? Please describe.
Some users would like to pass variables and functions down to Slices through the Slice Zone.
I need to pass several functions and variables to the slices, predominately for event listeners that will affect other areas on the website which are not slices.
So far, the only way I seem to be able to pass these properties to the slices is by using the ‘context’ parameter.
Describe the solution you’d like
Something like the sliceProps
property in next-slicezone
:
https://github.com/prismicio/slice-machine/tree/master/packages/next-slicezone#slicezone
Additional context
Forum Thread: https://community.prismic.io/t/passing-additional-properties-in-slicezone-with-sliceprops/6927/11
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Passing additional Properties in SliceZone with SliceProps
Hi, we're trying to pass additional data to the slices via the SliceZone with the sliceProps attribute. However, I"m not seeing any of...
Read more >next-slicezone
A component that fetches Prismic files, returns slices found and matches them with front-end components. RFC: https://github.com/prismicio/slice ...
Read more >[Feature] Custom props in next-slicezone · Issue #102
As user on the forum asked if you can pass custom props in next-slicezone , which I don't think is possible currently. Could...
Read more >prismic-reactjs - bytemeta
Add minimal Typescript example with repeatable fields. angeloashmore ... [RFC] Passing additional Properties in SliceZone with SliceProps. levimykel.
Read more >prismicio/prismic-react Issues
PrismicLink: Missing Link Properties error message isn't helpful enough ... [RFC] Passing additional Properties in SliceZone with SliceProps.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There are two recommended ways to pass data to Slice components. Using one of these two methods is preferred, in my opinion, over adding an additional
sliceProps
prop containing potentially complex logic.I am curious to hear if there is a use case where one of the two options cannot accomplish what you need. Maybe there is a need for another prop that I missed. 😃
1. Treat React components as functions
Pass Slice-specific data to the components in the
components
object. Because a React component is a function (we could also call it a “resolver”), something like thesliceProps
prop fromnext-slicezone
is built-in. For example:In this example, the
text
Slice will render a<TextSlice>
component with amyCustomProp
value. Because theprops
argument has all information about the slice, including the wholeslice
object, it can also be used to compute these custom props. Just remember to pass any used properties as props again.Here, the
theme
props is given a value derived from theslice
object.slice
is passed explicitly to the component so it can be used elsewhere.2. Pass arbitrary data to the
context
propAny data passed to the
context
prop will be passed to all Slices. How the data is used and how it is shaped is totally up to you depending on how you need to use it. One example is providing an object keyed by the Slice type that needs the data. This organizes data in a scalable way.If this custom data is only used in one or a few Slices, consider using the first method (“Treat React components as functions”) and pass the data directly to the component.
If you need this custom data in all Slices, then use the
context
prop.Great! Happy to hear it was helpful.
I’m going to close this feature request as I believe it has already been addressed. If you have any additional feedback or thoughts, please feel free to comment here and we can continue the discussion.
Thanks!