react-lifecycle

Refactoring class component to functional component with hooks, getting Uncaught TypeError: func.apply is not a function

夙愿已清 提交于 2021-01-29 20:37:31
问题 This is my first attempt to refactor code from a class component to a functional component using React hooks. The reason we're refactoring is that the component currently uses the soon-to-be-defunct componentWillReceiveProps lifecylcle method, and we haven't been able to make the other lifecycle methods work the way we want. For background, the original component had the aforementioned cWRP lifecycle method, a handleChange function, was using connect and mapStateToProps, and is linking to a

API call on event in react, componentDidUpdate or eventHandler?

℡╲_俬逩灬. 提交于 2021-01-28 05:29:36
问题 Where should we ideally place an api call to be made on occurrence of an event in React Inside the eventHandler or componentDidUpdate ? example: handleItemClick = (item) => (event) => { this.setState({selectedItem: item}); this.props.requestDataActionDispatch(item); } OR componentDidUpdate(prevProps, prevState, snapshot) { if(prevState.item !== this.state.item) { this.props.requestDataActionDispatch(item); } } 回答1: Depends But a simple solution is, if you want to call some API after change of

componentWillUnmount with React useEffect hook

感情迁移 提交于 2020-03-17 10:06:24
问题 How can the useEffect hook (or any other hook for that matter) be used to replicate componentWillUnmount ? In a traditional class component I would do something like this: class Effect extends React.PureComponent { componentDidMount() { console.log("MOUNT", this.props); } componentWillUnmount() { console.log("UNMOUNT", this.props); } render() { return null; } } With the useEffect hook: function Effect(props) { React.useEffect(() => { console.log("MOUNT", props); return () => console.log(

React app componentDidMount rendering twice

邮差的信 提交于 2020-01-06 07:27:30
问题 I've got a simple react app that has a Login that receives a Json Web Token on successful authentication and the passes it to a sibling component (Members) that in its componentDidMount uses this JWT to make a fetch call to the server. The thing is that componentDidMount is being called twice, the first one with undefined JWT and the second time with the retrieved JWT. Heres my code: App (parent code): class App extends Component{ state = { clientToken: '' } callbackGetToken = (token) => {

React app componentDidMount rendering twice

余生颓废 提交于 2020-01-06 07:26:39
问题 I've got a simple react app that has a Login that receives a Json Web Token on successful authentication and the passes it to a sibling component (Members) that in its componentDidMount uses this JWT to make a fetch call to the server. The thing is that componentDidMount is being called twice, the first one with undefined JWT and the second time with the retrieved JWT. Heres my code: App (parent code): class App extends Component{ state = { clientToken: '' } callbackGetToken = (token) => {

conditional async actions in componentDidMount lifecycle method keeps going on a loop

谁都会走 提交于 2020-01-05 03:57:04
问题 I am working on a react app using redux and sagas connected to an API. There's a form component that has two dropdown fields: a Program and a Contact field. The way the form is designed to work is, when the user selects a program, the form uses the programId to fetch all the contacts that have registered for that program. These contacts are then populated as the options for the contact dropdown field. This works and I've implemented it using the componentWillReceiveProps, like this:-

Props updated, componentDidUpdate did not fire

不羁岁月 提交于 2019-12-13 15:30:27
问题 Is there ever a situation where componentDidUpdate would NOT fire, even if the props updated in React? 回答1: There are 3 conditions that can cause componentDidUpdate to not fire: 1) You wrote a custom shouldComponentUpdate that returned false. 2) The internal react methods determined that there was no change in the virtual DOM, so it chose not to re-render. 3) You extended something other than React.Component (such as PureComponent) which has a default shouldComponentUpdate method that

componentDidMount is not working when redirect to a screen

若如初见. 提交于 2019-12-11 15:48:58
问题 I am having componentDidMount to list set of files(images) in a screen A, from this screen A I am having another screen B link where I can see detailed view of a file and there I have an option to delete a document. When screen A is called using <TouchableOpacity style={Styles.HomeButton} onPress={ () => this.props.navigation.navigate('Gallery')}> <Text style={Styles.HomeButtonText}>View Photos</Text> </TouchableOpacity> componentDidMount working fine, But when I delete a file (i am using