score:0
componentdidmount
function is called after the render and even though you have your call in componentwillmount
, since its an async call, it will only be resolved after the component render cycle has been triggered and hence the data will only have a value after render. in order to correctly handle such a scenario, you must conditionally render your data in the render.
class admin extends react.component {
state = {
access: null,
};
componentdidmount() {
this.unlisten = firebase.auth().onauthstatechanged(user => {
if (user) {
this.setstate(() => ({ access: true }));
}
});
}
componentwillunmount() {
this.unlisten();
}
render(){
const { access } = this.state;
if(access !== null) {
return null;
}
return <tab access={this.state.access}/>
}
}
score:1
you can do conditional rendering and not render the tabs until you get access:
return this.state.access
? <tab access={this.state.access}/>
: <div>not authorized</div>
score:1
it should not be a problem. when you update the state, the component will re-render and all its children will also be re-rendered. if you don't want to render anything while access is null try the following code.
class admin extends react.component {
state = {
access: null,
};
componentdidmount() {
this.unlisten = firebase.auth().onauthstatechanged(user => {
if (user) {
this.setstate(() => ({ access: true }));
}
});
}
componentwillunmount() {
this.unlisten();
}
render(){
const access = {this.state};
return {access && <tab access={access}/>}
}
}
or
{access ? <tab access={access}/> : 'not authorized'}
Source: stackoverflow.com
Related Query
- how to set react parent component state by a async function and then pass this state to child as props?
- How to make React.js fetch data from api as state and pass this state data from its parent to child component
- How to store react component in state and pass a callback function
- How Do I Pass a Function Through A React Component For Data Sharing Between Parent and Child?
- How can I redirect to another component in react and pass the state that I set in the previous component?
- In React, how can a parent component set state from the response an async fetch function performed in a child component?
- React Child component will mount then unmount after calling parent function to set parent state
- How to set state in parent from one child component and access the state in another child component using react and typescript?
- How to pass a state changing function to a component, and then pass that state to a parent component?
- How do I get a valeu from a async function and pass it to a component ( React )?
- How to set React component state and props from browser
- How to TEST async calls made in componentDidMount that set the state of React Component
- How to test state update and component rerender after async call in react
- How to make an http call in parent component to setState and then send the state to all child components?
- How to pass a function as props when both Parent and Child component also a functional component?
- How do i render multiple props in a single component and then pass those props to a child component in React
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js?
- Set value of prop in child component and pass value back to state of app.js, react
- How to pass a state to a Parent component if I have a button triggering the state in the 2nd Child in React
- How to set a loading state for react component while sorting and filtering data?
- Need Reactjs help on how to properly pass this function from the parent to child button component
- How to pass a prop back from child to parent in onclick handler and then to another component in React?
- React - How can I set my parent state using child component, using function structures (not classes)
- in React i need to get the state from local storage and then set another state conditionally based on this state
- How can I make this react component reusable and pass a dynamic initial state?
- How to fetch data from API and then pass it to another component in react after it is fully loaded?
- Async props pass in React: How to pass a key to update redux store in react? [First from the higher component and then from redux store]
- How to pass props or state from parent to child react in this format
- How to pass latest state to a Parent component from Child Component in react
More Query from same tag
- How do I group by a value in a nested object in ReactJS material-table?
- How to integrate react-draft-wysiwyg custom component with React-Final-Form
- Does [history.push] in useEffect dependency array cause rerenders?
- ReactJS & Redux: Can't perform a React state update on an unmounted component
- Updating states from input field when using React-Redux
- Nextjs getStaticProps error when passing an object as prop instead of plain string
- Creating a class in react and exporting with higher order components
- Next.js 9+ FOUC (Flash or Unstyled Content) with Styled Components
- Cannot create a new reactJs application
- React js.Changing button colours if user clicked on correct/incorrect options from API
- MERN delete data undefined at backend
- How can I update component in React after adding a new item into the database, using react hooks?
- Loop through an array of nested objects in javascript
- ReactJS async rendering
- ReactJS Semantic ui - Cannot type into form when value attribute exists
- React Input Type not editable
- Transform typescript specific statements to javascript
- Assign local storage to react state. React Hooks
- How to call child's method from parent without using Refs?
- React Select Option value when no change
- How to add material-ui component by innerHTML?
- I wonder why there are different results from errors when I use express, axios, and cors
- React router error (Failed prop type: Invalid prop `children` supplied to `Switch`, expected a ReactNode.)
- POST 400 (BAD REQUEST) when uploading file between React and Flask
- How to save pdf in react created using react-pdf library?
- Accessing array with Hooks and useState
- Update Redux state on route change
- Expo Crashing only on mobile, but works on Web output
- React Component Render function testing using Enzymes
- How to represent a book content in an array