0

I have an API that gives an error response in this form enter image description here

In reactJS I am able to pass the parameters by using this.props.errors to my child component, but now I want to map this.props.errors into the child component as below

state = {
 slug: '', 
 name: '', 
 address_1: '', 
 address_2: '', 
 city: '', 
 state: '', 
 postal_code: '', 
 country_code: '', 
 phone_number: '',
}

meaning, I want the elements of the error parameter to be placed in a child component's state per element. How do I do that?

Last
  • 721
  • 2
  • 7
  • 11
  • Please show us your parent component and how do you communicate with child component. – Liam Jul 06 '18 at 06:19
  • @Liam sorry changed the question when I noticed how stupid my error was as to why it won't pass the information – Last Jul 06 '18 at 06:37
  • Possible duplicate of [React component initialize state from props](https://stackoverflow.com/questions/40063468/react-component-initialize-state-from-props) – Ryan C Jul 06 '18 at 06:39

1 Answers1

0

Save your response in customizing as per your requirement. As per code snippet. save it in state and pass this state to children. by using componentWillReceiveProps update child state.

example: working Code with React

let response = {
      slug: ['slug required'],
      name: ['name'],
      address_1: ['address_1'],
      address_2: ['address_2'],
      city: ['city'],
      state: ['state'],
      postal_code: ['postal_code'],
      country_code: ['country_code'],
      phone_number: ['phone_number ']
    } 
    const keys = Object.keys(response)
    keys.map((m)=>{
      response[m] = Array.isArray(response[m]) ? response[m][0] : response[m]
    })
    console.log("customed response",response)
Revansiddh
  • 2,932
  • 3
  • 19
  • 33