I'm trying to dynamically set context in a parent component from a child component using this post as a guide
Here's my App.tsx:
export interface User {
username: string;
}
export interface iUserContext {
users: User[];
setUsers: React.Dispatch<React.SetStateAction<User[]>>;
}
function App() {
const [users, setUsers] = useState<User[]>([]);
const value = { users, setUsers };
// set defaults
const UsersContext = createContext<iUserContext>({
users: [],
setUsers: () => {},
});
return (
<div className="App">
<Router>
<UsersContext.Provider value={value}>
<Layout>
<Switch>
<Route path="/generated-links">
<GeneratedLinks />
</Route>
<Route path="/">
<GenerateLinks />
</Route>
</Switch>
</Layout>
</UsersContext.Provider>
</Router>
</div>
);
}
Then, in GenerateLinks.tsx:
const GenerateLinks = () => {
const history = useHistory();
const { users, setUsers } = useContext<iUserContext>(UsersContext);
The error I'm getting is for the UsersContext argument, just here ^:
Cannot find name 'UsersContext'. Did you mean 'useContext'?
I haven't used useContext and TS together before, so maybe I'm missing something obvious, but it seems like TS isn't detecting that I'm wrapping this component with the users context provider and passing in. Any ideas?