Skip to content

Instantly share code, notes, and snippets.

@swikars1
Last active August 23, 2023 13:58
Show Gist options
  • Select an option

  • Save swikars1/abbb77d68d209969cd480f758867f02a to your computer and use it in GitHub Desktop.

Select an option

Save swikars1/abbb77d68d209969cd480f758867f02a to your computer and use it in GitHub Desktop.
solution for conditional props react
import Component from "./Component";
import "./styles.css";
import { useState } from "react";
import { Check, Random } from "./random";
/**
* @Question
*Implement a theme switcher.that can switch between themes in entire application.
*/
export default function App() {
const [isDark, setIsDark] = useState(false);
const handleThemeChange = () => {
setIsDark((p) => {
return !p;
});
if (!isDark) {
document.body.classList.add("dark");
} else {
document.body.classList.remove("dark");
}
};
return (
<div className={`App ${isDark ? "dark" : ""}`}>
<h1>Velorona Frontend Technical Test</h1>
<Component />
<button
onClick={() => {
handleThemeChange();
}}
>
toggle
</button>
<div>
<Random success={true} data="success-> data" />
<Random success={false} error="no success -> error" />
</div>
</div>
);
}
/**
* codesandbox was aparently bugged out with this solution.
* tried same in local and was working fine.
*/
import React from 'react'
type TProps =
| {
success: true
data: string
}
| {
success: false
error: string
}
export function Random(props: TProps) {
return <div>{props.success ? props.data : props.error}</div>
}
.App {
font-family: sans-serif;
text-align: center;
}
.section {
width: 200px;
height: 400px;
border: 2px solid red;
}
.invervals {
display: flex;
width: 100%;
}
.dark {
background-color: black;
color: white;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment