I want to render two components ( TodoDone and TodoRemaining ) in this TodoDisplay component
The default render should be TodoRemaining but somehow there should be a onClick handler to render TodoDone component.
Can anyone suggest a way to achieve it ?
import React, { useState, useEffect } from "react";
import TodoRemaining from "../TodoRemaining/TodoRemaining";
import TodoDone from "../TodoDone/TodoDone";
import { DoneTodoProvider } from "../Context/DoneTodoContext";
const TodoDisplay = () => {
const [isPreview, setIsPreview] = useState(true);
if (isPreview) {
return (
<div>
<DoneTodoProvider>
<TodoRemaining />
</DoneTodoProvider>
</div>
);
} else {
return (
<DoneTodoProvider>
<TodoDone />
</DoneTodoProvider>
);
}
};
export default TodoDisplay;
Conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like if or the conditional operator to create elements representing the current state, and let React update the UI to match them. This example renders a different greeting depending on the value of isLoggedIn prop.
Conditional rendering is a term to describe the ability to render different user interface (UI) markup if a condition is true or false. In React, it allows us to render different elements or components based on a condition. This concept is applied often in the following scenarios: Rendering external data from an API.
We can create a functional component to React by writing a JavaScript function. These functions may or may not receive data as parameters. In the functional Components, the return value is the JSX code to render to the DOM tree.
You could do it like this:
const TodoDisplay = () => {
const [isPreview, setIsPreview] = useState(true);
if (isPreview) {
return (
<div>
<DoneTodoProvider>
<TodoRemaining />
</DoneTodoProvider>
<Button onClick={() => setIsPreview(false)}>flip</Button>
</div>
);
} else {
return (
<DoneTodoProvider>
<TodoDone />
</DoneTodoProvider>
<Button onClick={() => setIsPreview(true)}>flip</Button>
);
}
};
export default TodoDisplay;
A cleaner way:
return (
<div>
<DoneTodoProvider>
{isPreview?
<TodoRemaining />
:
<ToDoDone />
}
</DoneTodoProvider>
<Button onClick={() => setIsPreview(!isPreview)}>flip</Button>
</div>
);
You could add a checkbox to toggle isPreview
and call it on like a checkbox's change event.
const { useState, useEffect } = React;
const TodoRemaining = () => <div>Remaining</div>;
const TodoDone = () => <div>Done</div>;
const TodoDisplay = () => {
const [isPreview, setIsPreview] = useState(true);
return (
<div>
{isPreview ? <TodoRemaining /> : <TodoDone />}
<label htmlFor="change">
<input
type="checkbox"
id="change"
onChange={() => setIsPreview(!isPreview)}
/>
Change
</label>
</div>
);
};
ReactDOM.render(<TodoDisplay />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
<div id="root"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With