[英]blank table in the attempted use
part of the problem is that the value entered in the input is exactly what I mean by the returned element, which is "[ ]" the expected result was to be input: Name output: Name + img etc, there is no error during the output and the returned message in the console is only [] it looks like something was missing in advance thanks for the help:)部分问题是输入中输入的值正是我所说的返回元素的意思,即“[]”预期的结果是要输入:名称output:名称+ img等,在此过程中没有错误output 并且控制台中返回的消息只是 [] 看起来好像缺少一些东西,谢谢您的帮助:)
App.js
<div>
<Nav />
<DataContext>
<Switch location={location} key={location.pathname}>
{routes.map(({ path, Component }) => (
<Route
key={path}
exact
path={path}
component={Component}
/>
))}
</Switch>
</DataContext>
</div>
//Data.js
export const DataContext = createContext();
const url = `https://corona.lmao.ninja/v2/countries`;
function Data(props) {
const [state, setState] = useState({ countries: [] });
useEffect(() => {
axios.get(url).then((res) => {
const countries = res.data;
setState({ countries });
});
}, []);
return (
<DataContext.Provider value={{ state, setState }}>
{props.children}
</DataContext.Provider>
);
}
export default Data;
Countries.js
const Div = styled.div`
padding: 10vh 0 0 0;
`;
function Countries() {
const [find, setFind] = useState("");
const [filteredData, setFilteredData] = useState([]);
const dataContext = useContext(Data.DataContext);
const { state } = dataContext;
const content = state.countries;
useEffect(() => {
setFilteredData(
content.filter((country) =>
country.toString().toLowerCase().includes(find)
)
);
}, [find, content]);
return (
<Div>
<div>
<input
type="text"
placeholder="Search"
value={find}
onChange={(e) => setFind(e.target.value)}
/>
</div>
<div>
{filteredData.map((r, i) => (
<div key={i}>
<Card style={{ width: "18rem" }}>
<Card.Img src={r.countryInfo.flag} variant="top" />
<Card.Body>
<Card.Title>{r.country}</Card.Title>
<Link to={`/${r.country}`}>
<Button variant="primary">Visit</Button>
</Link>
</Card.Body>
</Card>
</div>
))}
</div>
</Div>
);
}
export default Countries;
problem solved问题解决了
useEffect(() => {
const filter = content.filter((state) => {
return state.country
.toLowerCase()
.includes(find.toLocaleLowerCase());
});
setFilteredData(filter);
}, [find, content]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.