score:1
Accepted answer
Without the full code, we can't tell what's wrong but it is obvious that following_status
comes asynchronously to the component and that's why is not accessible right away in the constructor.
To somehow fix it you can detect if props have changed and reset state accordingly in componentDidUpdate
.
class RelationButton extends React.Component {
constructor(props){
super(props);
console.log(props.user.following_status) # undefined!!!
this.state = {
following_status: props.user.following_status
}
}
componentDidUpdate(prevProps) {
if(prevProps.user.following_status !== this.props.user.following_status) {
this.setState({ following_status: this.props.user.following_status })
}
}
render() {
// you forgot about return statements :
if (this.state.following_status == 'following') {
return <UnFollowBtn/>
} else {
return <FollowBtn/>
}
}
}
Source: stackoverflow.com
Related Query
- Get default state value by receiving prop data - React
- Providing default value for function prop in React
- Get state value by a dynamic key in react
- How to assign a prop value to a state in react
- Trying to get a React controlled input to have a default value
- How can mock the value of a state and data in my react test
- get latest state value after setState hook within async await block in react
- Default function parameter value from React component state
- how to calculate default value for react js state hook with a function?
- How to set the default react context value as data from firestore?
- How to Assign react redux state api get data using axios
- How to use an enum in typescript as a default prop value for a react component
- using state value in state data for apex charts within react app
- Set value of prop in child component and pass value back to state of app.js, react
- React - How to get state data from a child component?
- How can I set the default state of a checkbox based on value in firestore in react table
- Passing a state or prop to pass a value to child component in react
- setting default value for react class component state in typescript
- React Recoil selector not triggering its get when the atom state updates to a previous value
- On React hook component unmount, unable to get updated state variable value
- React JS Get State Value from another State
- React state is called before receiving value from API
- How to get checkbox value immediately after setting the state in react
- Get prop value from div in React
- Filtering Json data based on key and assiging the value to state in react
- React useCallback hook, not receiving updated dependency state value
- React component doesn't get current state value in Event Listener function
- Get Data from a List of Firestore DocumentReferences to State React
- How to update the value of a prop upon a state change in React
- Can't get the array state value in React
More Query from same tag
- Reducer always returning initial state React-native
- Not able to fetch data from graphQL in gatbsy
- Google Login Button Does Not Render in React
- React Array of components - click handler issue
- How to clear interval placed inside .map callback function
- How to remove imported css in reactjs
- React-Boostrap Carousel indicator not aligned
- React Router history.goBack doesn't work when using <Redirect />
- TypeScript 2.6 -> material-ui 1.0.0-beta.24 withStyles with react-router withRouter -> Property 'classes' is missing in type
- What is the best approach for adding class and styles without re-render - reactjs
- ReactDom.Render() in SharePoint Framework App
- How to get the first day of the next month in JavaScript
- How to get facebook uid in firebase using react?
- How to update an existing value in state array without mutation?
- How to send the data to server in react?
- How to change routes in gatsby without Link component?
- React: how to get item width from map function
- Initializing PerfectScrollbar in React app
- What is wrong in the below code of input validation?
- How to make create-react-app check for unused imports
- Firebase React. How can I access my Auth values in React / Webpack?
- If useEffect is not called, do something else React.js
- React State listen for Object Attribute Change
- Propagate function error from parent component to child component in React while making API request
- Search is not working in mui-datatable, reactjs?
- How to display an error message when fields are empty - React
- How to migrate app to react-redux v6 and v7 with context API
- React Hooks : fetch data inside useEffect warning
- React localStorage issue
- Call method passed from redux in react from child to parent