JavaScript React passing new props react passing props to parent,react passing props to props.

So I update my state in a component and then pass the new props into the child but the child isn't updating correctly and the defaultValue of the input is not changing. At first I thought it might be because I am using this.props so begun using this.states and applying the new props there first but doesn't seem to be working.

Parent Component

this.state.newDetails == null ? '' : 
    <NewDetailsView details={this.state.newDetails} /> 

Child component:

import React, { Component } from 'react';

class NewDetailsView extends Component {
  constructor(props) {
    super(props);
    this.state = {
      details: (this.props.details!= null) ? this.props.details: null
    }
  }

  componentWillReceiveProps(nextProps) {
    this.setState({ details: nextProps });
    this.forceUpdate();
  }

  render() {
    return (
      <div>
        <input type="text" defaultValue={this.state.details.id} />
      </div>
    );
  }
}

export default NewDetailsView ;

Solution Code:

Pending...

Answer:1

Issue is inside the componentWillReceiveProps:

this.setState({ details: nextProps });

it should be :

this.setState({ details: nextProps.details });

And also remove this.forceUpdate(); , there is no need of forceUpdate here.


Sultion to second issue change defaultValue to just value :

<input type="text" value={this.state.details.id} />

Here is the link to working example :

https://stackblitz.com/edit/react-parent-child-prop

Answer:2
const NewDetailsView = ({details}) => (
  <div>
    <input type="text" value={details? details.id: null} />
  </div>
);
Answer:3

Use getDerivedStateFromProps instead of using deprecated componentWillReceiveProps. An example of it can be found here

Answer:4

I have been reading about AMD and implementations like requirejs. Most of the resources covers the usage and API's. But, when it comes to implementing this, how can you load a JavaScript file into a ...

I have been reading about AMD and implementations like requirejs. Most of the resources covers the usage and API's. But, when it comes to implementing this, how can you load a JavaScript file into a ...

i m trying to remove tr from table using closest function. this function is working properly out of the $.post request but when same script is used in post request its not working. My Code is $("....

i m trying to remove tr from table using closest function. this function is working properly out of the $.post request but when same script is used in post request its not working. My Code is $("....

  1. didn't
  2. https://www.disneyplus.com/
  3. didn't i
  4. didn't i lyrics
  5. disney plus
  6. didn't i onerepublic
  7. didnt even notice
  8. didn't ask
  9. disney store
  10. disney world
  11. disneyland
  12. didn't synonym
  13. didn't ask meme
  14. disneyland tickets
  15. disney +
  16. didn't know i was pregnant
  17. didn't detect another display
  18. didn't cha know
  19. disney cruise
  20. disney movies

I have an input field where I expect users to type text that contains 1 of many keywords that will trigger different audio files depending on the keyword. (I know that's not really smart from UX point ...

I have an input field where I expect users to type text that contains 1 of many keywords that will trigger different audio files depending on the keyword. (I know that's not really smart from UX point ...

I have written Backbone view - ListView and want to display table in to it. For individual table rows, there is another view - EmployeeView. I am binding click event on delete button which is inside ...

I have written Backbone view - ListView and want to display table in to it. For individual table rows, there is another view - EmployeeView. I am binding click event on delete button which is inside ...

  1. event in backbone view
  2. backbone event click outside view
  3. trigger event in backbone view