I'm a beginner in React and stuck with some problem. I have two components. one is parent(App.js) which passes its state to child component(CardList). the other is child which receives passed state and use it as props. What I wanted to do is when I click the search button and give some username it will search data of the relevant username and I'll append to the state array defined in parent functional component and in child, I sent it as props and In child component I'll iterate over the list of props.stateArray and print the corresponding data. Even I have put the console.log() to check whether the flow goes to this child component or not but this console runs only when 1st render calls after that every time when the state change the console won't log any message.Please find the image below:-enter image description here
Parent Component **App.js**
import React, { useState } from 'react';
import Form from './Form';
import CardList from './CardList';
export default function App() {
const [profiles, setProfiles] = useState([]);
const addNewProfile=(profile)=>{
profiles.push(profile);
setProfiles(profiles);
console.log(profiles);
}
return (
<>
<Form addProfile={addNewProfile}/>
<CardList profilese={profiles}/>
</>
);
}
Child Component -- **CardList.js**
import React from 'react';
import Cardi from './Cardi';
export default function CardList(props)
{
console.log("Everytime State of Parent change");
return(
<div>
{props.profilese.map(profile => <Cardi key={profile.id} {...profile}/>)}
</div>
);
}
Another Child Component -- **Form.js**
import React,{useState} from 'react';
import axios from 'axios';
import regeneratorRuntime from "regenerator-runtime";
export default function Form(props)
{
const[username,setUsername]=useState("");
const handleSubmit=async(event)=>{
event.preventDefault();
try{
const resp= await axios.get
(`https://api.github.com/users/${username}`);
const data=await resp.data;
props.addProfile(data);
}catch(err)
{
console.log("Exception Occured"+ err);
}
}
return(
<form onSubmit={handleSubmit}>
<input type="text"
value={username}
onChange={event=>
{setUsername(event.target.value)}}
/>
<button >Search</button>
</form>
);
}
**Cardi.js**
import React from 'react';
export default function Cardi(props)
{
console.log(props.profile);
if(props.profile===undefined)
{
return (
<div>Can't find that username</div>
);
}
else{
return (
<>
<img src={props.profile.avatar_url}/>
<div className="info">
<div className="name">{props.profile.name}</div>
<div className="company">{props.profile.company}</div>
</div>
</>
);
}
}