I'm trying to pass props from my Parent ponent to child ponent. Here are the important snippets:
Snippet 1: This is the object that contains the prop (integer).
const cardProps = {
cardProps0: 0,
Snippet 2: This is the Card ponent within the parent ponent that carries the prop to child ponent
return (
<MyCardLink source={cardProps.cardProps0} />
Snippet 3: This is the child ponent (MyCardLink)
useEffect((props) => {
axios
.get(
';apiKey=XXXXXXXXXXXXXXXX'
)
.then((res) => {
console.log(res);
setNews(res.data.articles[props.source]);
})
.catch((err) => {
console.log(err);
});
}, []);
The goal is that [prop.source] contains a number value from a list of an array served by an API. If I just place a number value in the child ponent (MyCardLink) in place of [props.source] on the setNews function then it renders the ponent no problem.
My problem is when I pass the prop from parent ponent to child ponent and use [prop.source], nothing renders and all I get from the console log is:
Cannot read property 'source' of undefined.
Am I doing something wrong?
I'm trying to pass props from my Parent ponent to child ponent. Here are the important snippets:
Snippet 1: This is the object that contains the prop (integer).
const cardProps = {
cardProps0: 0,
Snippet 2: This is the Card ponent within the parent ponent that carries the prop to child ponent
return (
<MyCardLink source={cardProps.cardProps0} />
Snippet 3: This is the child ponent (MyCardLink)
useEffect((props) => {
axios
.get(
'http://newsapi/v2/everything?q=economy&apiKey=XXXXXXXXXXXXXXXX'
)
.then((res) => {
console.log(res);
setNews(res.data.articles[props.source]);
})
.catch((err) => {
console.log(err);
});
}, []);
The goal is that [prop.source] contains a number value from a list of an array served by an API. If I just place a number value in the child ponent (MyCardLink) in place of [props.source] on the setNews function then it renders the ponent no problem.
My problem is when I pass the prop from parent ponent to child ponent and use [prop.source], nothing renders and all I get from the console log is:
Cannot read property 'source' of undefined.
Am I doing something wrong?
Share Improve this question edited Sep 30, 2020 at 16:35 norbitrial 15.2k10 gold badges39 silver badges64 bronze badges asked Sep 30, 2020 at 16:27 PacholoamitPacholoamit 2656 silver badges18 bronze badges2 Answers
Reset to default 4Instead of passing props
into your useEffect
, you need to add into your MyCardLink
ponent's parameters as:
const MyCardLink = (props) => {
// your ponent's defintion
}
Additionally you can destructure as the following:
const MyCardLink = (props) => {
const { source } = props
// ... rest
}
Then simply you can use in your useEffect
without props
as:
useEffect(() => {
axios.get(
'http://newsapi/v2/everything?q=economy&apiKey=XXXXXXXXXXXXXXXX'
)
.then((res) => {
console.log(res);
setNews(res.data.articles[source]);
})
.catch((err) => {
console.log(err);
}
);
}, []);
Based on your other question from the ment section what I would do is:
- Change the initial value of the state from
""
tonull
asconst [news, setNews] = useState(null)
. - Also I would use
&&
fornull
check and render<Card />
ponent only if it has value asnews && <Card className={classes.root}>
in yourreturn
.
The reason behind this is your API response is arriving asynchronously.
use use props in ponent below:
const MyCardLink =(props)=>{
...
...
...
}
export default MyCardLink;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745459666a4628647.html
评论列表(0条)