Visibility
Conditionally show or hide components based on data, auth, or logic.
VisibilityProvider
Wrap your app with VisibilityProvider to enable conditional rendering:
import { VisibilityProvider } from '@json-render/react';
function App() {
return (
<DataProvider initialData={data}>
<VisibilityProvider>
{/* Components can now use visibility conditions */}
</VisibilityProvider>
</DataProvider>
);
}Path-Based Visibility
Show/hide based on data values:
{
"type": "Alert",
"props": { "message": "Form has errors" },
"visible": { "path": "/form/hasErrors" }
}
// Visible when /form/hasErrors is truthyAuth-Based Visibility
Show/hide based on authentication state:
{
"type": "AdminPanel",
"visible": { "auth": "signedIn" }
}
// Options: "signedIn", "signedOut", "admin", etc.Logic Expressions
Combine conditions with logic operators:
// AND - all conditions must be true
{
"type": "SubmitButton",
"visible": {
"and": [
{ "path": "/form/isValid" },
{ "path": "/form/hasChanges" }
]
}
}
// OR - any condition must be true
{
"type": "HelpText",
"visible": {
"or": [
{ "path": "/user/isNew" },
{ "path": "/settings/showHelp" }
]
}
}
// NOT - invert a condition
{
"type": "WelcomeBanner",
"visible": {
"not": { "path": "/user/hasSeenWelcome" }
}
}Comparison Operators
// Equal
{
"visible": {
"eq": [{ "path": "/user/role" }, "admin"]
}
}
// Greater than
{
"visible": {
"gt": [{ "path": "/cart/total" }, 100]
}
}
// Available: eq, ne, gt, gte, lt, lteComplex Example
{
"type": "RefundButton",
"props": { "label": "Process Refund" },
"visible": {
"and": [
{ "auth": "signedIn" },
{ "eq": [{ "path": "/user/role" }, "support"] },
{ "gt": [{ "path": "/order/amount" }, 0] },
{ "not": { "path": "/order/isRefunded" } }
]
}
}Using in Components
import { useIsVisible } from '@json-render/react';
function ConditionalContent({ element, children }) {
const isVisible = useIsVisible(element.visible);
if (!isVisible) return null;
return <div>{children}</div>;
}Next
Learn about form validation.