Exporting default props
Exporting Default Props in a Block
Summary
- Overview
- Current Scenario
- Exporting Default Props in Function Parameters
- Implementation
- Conclusion
Overview
This document details the current behavior of default values in components within the Admin and introduces a new feature that allows developers to specify default props directly within function parameters. This enhancement improves user experience by ensuring that default values are reflected correctly in both the Admin interface and the rendered components.
Current Scenario
In the current setup, default values for props are hardcoded within the component’s code. This leads to a mismatch between the Admin interface and the rendered component:
- Admin: When a component has default values set in its code, the form in the Admin interface displays empty fields instead of these default values.
- Rendered Component: Despite the empty form fields, the component still renders with the hardcoded default values.
- JSON: The JSON file displayed to the developer in the Admin interface doesn’t include these default values, leading to confusion and a poor user experience.
Example
Consider the following scenario: A footer component has default props defined in its code. In the Admin interface, the form fields for these props are empty, but the component is rendered with the hardcoded values. This disconnect makes it difficult for developers and content managers to map input text to the page data accurately.
Exporting Default Props in Function Parameters
To address this issue, the Admin now supports specifying default props directly within the function parameters of any block. This ensures that default values are consistently reflected in the interface, the rendered component, and the JSON file shown to developers, leading to benefits like:
- Consistency: Default values are visible and editable in the admin interface, ensuring a consistent user experience.
- Clarity: Developers can see the actual default values in the JSON file, reducing confusion.
- Simplicity: Easier to manage and update default values directly within the function parameters.
This feature supports simpler scenarios, covering basic data types and nested objects.
Implementation
To specify default props in your component, include them directly within the function parameter itself. Below is an example of how to implement this:
Explanation
- Default Prop Values: The default values for image, href, text, alt, height, width, and list are specified directly in the function parameter.
- Props Interface: The Props interface defines the structure and types of the props.
- Component Logic: The component logic uses these default values, ensuring they are applied consistently.
So the Admin props, the preview, the code and the JSON file should look like the following image, instead of having empty values defined:
Conclusion
By specifying default props directly in the function parameters, this new feature improves the integration between the admin interface and the component rendering. This change simplifies the workflow for developers and enhances the overall user experience by ensuring that default values are visible and manageable across all parts of the Admin.
Was this page helpful?