score:0
Conditional rendering using state.
In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application.
Working example (click on the Dashboard
tab):
containers/Dashboard
import isEmpty from "lodash/isEmpty";
import React, { Component } from "react";
import api from "../../utils/api";
import DisplayUser from "../../components/DisplayUser";
import DisplaySignUp from "../../components/DisplaySignUp";
import Spinner from "../../components/Spinner";
class Dashboard extends Component {
state = {
isLoading: true,
currentUser: {}
};
componentDidMount = () => {
this.fetchUsers();
};
fetchUsers = async () => {
try {
const res = await api.get(`/users/1`);
setTimeout(() => {
this.setState({ currentUser: res.data, isLoading: false });
}, 1500);
} catch (e) {
console.error(e.toString());
}
};
// the below can be read like so:
// if "isLoading" is true... then display a spinner
// else if "currentUser" is not empty... then display the user details
// else show a signup message
render = () =>
this.state.isLoading ? (
<Spinner />
) : !isEmpty(this.state.currentUser) ? (
<DisplayUser currentUser={this.state.currentUser} />
) : (
<DisplaySignUp />
);
}
export default Dashboard;
score:0
For what you intend to do, just adding the hidden
prop won't work as that is not an expected attribute of the Spinner
component. I think what you need to do is to introduce conditional rendering in your component. Kindly see implementation below:
import * as React from 'react';
import { render } from 'react-dom';
import {
PrimaryButton,
Spinner,
SpinnerSize,
Label,
IStackProps,
Stack
} from 'office-ui-fabric-react';
import './styles.css';
const { useState } = React;
const SpinnerBasicExample: React.StatelessComponent = () => {
// This is just for laying out the label and spinner (spinners don't have to be inside a Stack)
const rowProps: IStackProps = { horizontal: true, verticalAlign: 'center' };
const tokens = {
sectionStack: {
childrenGap: 10
},
spinnerStack: {
childrenGap: 20
}
};
return (
<Stack tokens={tokens.sectionStack}>
<Stack {...rowProps} tokens={tokens.spinnerStack}>
<Label>Extra small spinner</Label>
<Spinner size={SpinnerSize.xSmall} />
</Stack>
<Stack {...rowProps} tokens={tokens.spinnerStack}>
<Label>Small spinner</Label>
<Spinner size={SpinnerSize.small} />
</Stack>
</Stack>
);
};
function App() {
const [hidden, setHidden] = useState(false);
return (
<div className="App">
{hidden && <SpinnerBasicExample />}
<PrimaryButton
data-automation-id="test"
text={!hidden ? 'Show spinner' : 'Hide spinner'}
onClick={() => setHidden(!hidden)}
allowDisabledFocus={true}
/>
</div>
);
}
score:3
You need to use conditional rendering to hide/show the spinner. You can define a component state then can set it up to false or true depending on if you want to show it or not.
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
hidden: false
};
}
render() {
return (
<div className="App">
{!this.state.hidden && <SpinnerBasicExample />}
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
For more you can read this https://reactjs.org/docs/conditional-rendering.html
Source: stackoverflow.com
Related Query
- How to hide spinner component
- <Fade> in material-ui just disables the visibility of the component . How to get the fade effect and actually hide the component ?
- How to hide component instead of unmounting it?
- How to hide header component in react-router-dom?
- how to hide and show loading spinner - Activity Indicator react native, managing props and state
- React - how to use hooks to hide or show a component based on an onChange event
- How can I hide a component in React depending on the screen size?
- React: How to hide a component by clicking on it?
- React: How to show Spinner & Component (at the same time) when State Changes using `useEffect`
- React JS - How can I show / hide some parts of component based on URL path?
- How to load a CSS spinner over my react component with opacity of 0.7?
- How to hide Footer component for some pages in react
- How can i hide some Navbar and Sidebar on some selected component
- How to hide a button component on a particular route in react-router
- How to show and hide a Modal in child component from Parent component?
- How to hide parent component when routing to nested path v6 reactjs?
- How can I hide header component in Login page
- I want to show and hide a form on toggle of a radio button in React js . Im trying how to use react hooks to hide or show a component on onChange even
- How to hide nav bar component in reactjs using protected route
- How to hide url by react-router, so component should not be visible
- I have a toggle in the main component (show & hide a div), but I want to move the button that activates it to a secondary component. how do I do it?
- How can I hide component (Navbar) on landing page
- How to hide my NavigationBar Component when on certain paths
- React-Router: how to hide Home page when linked component is opened?
- How to show / hide component with onClick set on element from other component
- react-TS how to hide login component if you already logged in
- How to hide a child component when changing React route
- How can I detect if a React component is completely loaded to show ot to hide a section?
- How to hide some component based on some flag in react js
- how to hide a component on specific routes in react.js
More Query from same tag
- Adding dynamic meta tags to a React static site, created using StaticSiteGeneratorPlugin
- Ant Design for react native truly native components?
- React Toggle open and close using onclick on map function
- eslint-plugin-jsx-a11y for Typescript
- React reference to my form's value attribute is not updating
- React js Execute function on submit
- React router nested routes. Not rendering child routes
- How to hide searchbar on another page
- React fetch and setting state
- How would you implement shouldComponentUpdate using useEffect in React?
- Handling Axios error in React
- Chrome extension Tailwind CSS interferes with websites I visit
- Should I be using React State for user-interactions (to toggle visibility classes)?
- Increment React Counters in a mapped list
- Handling hover in React
- React multiple children sends the same props
- React how to test function called in FileReader onload function
- React How to hide scrollbar for just y axis
- How to use conditional rendering by creating custom division returning functions with onClick attribute in submit button in React?
- promise chaining reject
- Associating conditional statements with buttons rendering in reactjs
- React-Select custom input inside MenuList
- Redux-Saga & Redux-Toolkit Wiring
- can I store a react component to a state? so I can load it dynamically
- Trying to return a value from a fetch request and populating using react hooks
- why this.state.text is throwing an error in function?
- React state hook issue
- React onClick function not executing when DEPLOYED
- How to ensure parent component passes the same prop function to child to avoid rerender
- React Dropdown mapped with empty values