时间:2021-07-01 10:21:17 帮助过:4人阅读
Provider it:
// index.js // React import React from "react"; import ReactDOM from "react-dom"; // CSS import "normalize-css"; import "./css/styles.css"; // Components import App from "./App"; import { NamesProvider } from "./providers/names"; import { AppStateProvider } from "./providers/app-state"; ReactDOM.render( <React.StrictMode> <NamesProvider> <AppStateProvider> <App /> </AppStateProvider> </NamesProvider> </React.StrictMode>, document.getElementById("root") );
Consume it:
import React from "react"; import { NamesList } from "./names-list"; import { useNames } from "../providers/names"; import { useAppState } from "../providers/app-state"; export function NamePicker() { const names = useNames(); const { searchValue, shortList, setShortList } = useAppState(); const filteredNames = names .filter(entry => entry.name.toLowerCase().includes(searchValue.toLowerCase()) ) .filter(entry => !shortList.includes(entry.id)); function addToShortList(id) { setShortList([...shortList, id]); } return <NamesList namesList={filteredNames} onItemClick={addToShortList} />; }
[React] Improve developer experience for accessing context with a custom React hook
标签:UNC you lis picker any styles sim iss ons