Last active
August 23, 2023 13:58
-
-
Save swikars1/abbb77d68d209969cd480f758867f02a to your computer and use it in GitHub Desktop.
solution for conditional props react
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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> | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .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