我在其中一个组件中有这个道具,我想将其传递给另一个组件并在那里用作变量:
const VerContinentToolbar = (props) => {
return (
<Menu className="nav-icon">
<CountryList displayFields={["countryName"]} />
</Menu>
);
};
如果我console.log(props.props),我会得到: https://imgur.com/a/HnRHDcH 这是我想传递给useCountries的值。 接收组件如下所示:
const useCountries = (props) => {
const [countries, setCountries] = useState([])
useEffect(() => {
firebase
.firestore()
.collection("Africa")
.onSnapshot((snapshot) => {
const newCountries = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data()
}))
setCountries(newCountries)
})
}, [])
return countries
}
const CountryList = ({ displayFields = [] }) => {
const countries = useCountries();
console.log(countries)
return (
<div className="countries">
{countries.map(country => (
<div key={country.id}>
<div className="entry">
{displayFields.includes("continent") && (
<div>Name of continent: {country.continent}</div>
)}
{displayFields.includes("revName") && (
<div>{country.revName}</div>
)}
{displayFields.includes("countryName") && (
<div><Link to={"./Jumbotron"}>{country.countryName}</Link></div>
)}
{displayFields.includes("dest1") && (
<div>Destination 1: {country.dest1}</div>
)}
{displayFields.includes("dest2") && (
<div>Destination 2: {country.dest2}</div>
)}
{displayFields.includes("dest3") && (
<div>Destination 3: {country.dest3}</div>
)}
{displayFields.includes("beerPrice") && (
<div>Beer price: {country.beerPrice}</div>
)}
{displayFields.includes("foodPrice") && (
<div>Food price: {country.foodPrice}</div>
)}
{displayFields.includes("hostelPrice") && (
<div>Hostel price: {country.hostelPrice}</div>
)}
{displayFields.includes("review") && <div>Review: {country.review}</div>}
{displayFields.includes("imgUrl") && <img src={country.url} alt="no-img" />}
</div>
</div>
))}
</div>
);
};
我已经尝试并试图理解上下文,但是我无法真正地绕开它。我想将props的值放入“ .collection(“ Africa”)“中,但是我不知道该怎么做。我已经阅读了它的文档,但是我实在太笨了,无法理解如何正确地解决它。
如果还有其他解决方案,我会很高兴。
我想要传递的道具是:我为每个大陆创建了一个组件。在每个组件中,我调用VerContinentToolbar,传递大陆的prop /名称,如下所示:
const Africa = ({}) => {
return (
<div>
<VerContinentToolbar props={["Africa"]} />
This means, that
const VerContinentToolbar = (props) => {
return (
<Menu className="nav-icon">
<CountryList displayFields={["countryName"]} />
</Menu>
);
};
保留“非洲”一词。
我想将“非洲”(Africa)传递给我的组件,该组件可以获取非洲大陆的集合(在本例中为非洲)。
我想将道具持有的信息传递给此:
const useCountries = (props) => {
const [countries, setCountries] = useState([])
useEffect(() => {
firebase
.firestore()
.collection("Africa") <---- I WANT PROP FROM VERCONT HERE
.onSnapshot((snapshot) => {
const newCountries = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data()
}))
setCountries(newCountries)
})
}, [])
return countries
}
为进一步说明这一点,我附加了两张图片。亚洲:在这里,我要呈现“亚洲”收藏集,以显示属于亚洲大陆内某个国家的每条评论。
到目前为止,我已经创建了“ Imagination land”并将其附加到Firebase中的“ Africa”。我只想在非洲部分展示一下。显然,我想在亚洲部分中显示所有带有亚洲标签的国家。
谢谢
编辑: 问题解决了。
import React, { Component, useState, useEffect } from 'react'
import { Link } from 'react-router-dom';
import firebase from '../config'
import './Countries.css'
import props from './Navbar/VerContinentToolbar';
const useCountries = continent => {
const [countries, setCountries] = useState([]);
console.log('continent', continent) // <--- Get it as your first argument
console.log(continent.toString())
useEffect(() => {
firebase
.firestore()
.collection(continent.toString())
.onSnapshot((snapshot) => {
const newCountries = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data()
}))
setCountries(newCountries)
})
}, [])
return countries
}
const CountryList = ({ continent, displayFields = [] }) => {
const countries = useCountries(continent); // <--- Pass it in here
return (
<div className="countries">
{countries.map(country => (
<div key={country.id}>
<div className="entry">
{displayFields.includes("continent") && (
<div>Name of continent: {country.continent}</div>
)}
{displayFields.includes("revName") && (
<div>{country.revName}</div>
)}
{displayFields.includes("countryName") && (
<div><Link to={"./Jumbotron"}>{country.countryName}</Link></div>
)}
{displayFields.includes("dest1") && (
<div>Destination 1: {country.dest1}</div>
)}
{displayFields.includes("dest2") && (
<div>Destination 2: {country.dest2}</div>
)}
{displayFields.includes("dest3") && (
<div>Destination 3: {country.dest3}</div>
)}
{displayFields.includes("beerPrice") && (
<div>Beer price: {country.beerPrice}</div>
)}
{displayFields.includes("foodPrice") && (
<div>Food price: {country.foodPrice}</div>
)}
{displayFields.includes("hostelPrice") && (
<div>Hostel price: {country.hostelPrice}</div>
)}
{displayFields.includes("review") && <div>Review: {country.review}</div>}
{displayFields.includes("imgUrl") && <img src={country.url} alt="no-img" />}
}
</div>
</div>
))}
</div>
);
};
export default CountryList
const VerContinentToolbar = (props) => {
return (
<Menu className="nav-icon">
<CountryList
continent={props.continent}
displayFields={["countryName"]}
/>
</Menu>
);
};
感谢您的所有帮助。
答案 0 :(得分:2)
从您的大陆组成部分将其作为道具(大陆)传递
const Africa = () => {
return (
<div>
<VerContinentToolbar continent="Africa" />
</div>
);
};
在<VerContinentToolbar />
中,获得道具并将其传递到<CountryList />
。与其将道具传递给子孙组件,还可以使用context API或Redux。
const VerContinentToolbar = props => {
console.log('continent prop is now here', props.continent)
return (
<Menu className="nav-icon">
<CountryList continent={props.continent} displayFields={["countryName"]} />
</Menu>
);
};
在<CountryList />
中,获取道具(continent
)并将其传递到您的useCountries
钩子中。
const CountryList = props => {
const countries = useCountries(props.continent); // <--- Pass it in here
console.log(countries)
return (
<div className="countries">
{countries.map(country => (
{/* ...code */}
))}
</div>
);
};
最后,将其作为第一个参数
const useCountries = continent => {
const [countries, setCountries] = useState([]);
console.log('continent', continent) // <--- Get it as your first argument
// ...code
return countries
}
答案 1 :(得分:1)
据我所知,您想在CountryList
内调用useCountries
钩子来指定所需的集合。
const useCountries = (collection) => {
const [countries, setCountries] = useState([])
useEffect(() => {
firebase
.firestore()
.collection(collection) // <-- pass collection argument here
.onSnapshot((snapshot) => {
const newCountries = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data()
}))
setCountries(newCountries)
})
}, [])
return countries
}
在CountryList
中将您想要的值传递给useCountries
const countries = useCountries('Africa');