admin管理员组文章数量:1425871
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;
本文标签: javascriptPassing props to UseEffect hook from Parent componentStack Overflow
版权声明:本文标题:javascript - Passing props to UseEffect hook from Parent component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745459665a2659263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论