Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  21] [ 7]  / answers: 1 / hits: 24488  / 5 Years ago, tue, december 3, 2019, 12:00:00

I can't understand why it gives me the above error. I used the props way with props.match.params.languagename and it works just fine.



I did not include all imports in the code below.




import { useParams } from 'react-router';

const App = () => {
const topicsState = useSelector(state => state.topics);

const dispatch = useDispatch();
const { languagename } = useParams();

useEffect(() => {
dispatch(fetchGitHubTrendingTopics());
}, [dispatch]);

const handleRepositoryPages = () => {
const repositoryPages = topicsState.find(
topicState => topicState.name === languagename
);
if (repositoryPages)
return <RepositoryPage repositoryPages={repositoryPages} />;
};
return (
<>
<Router>
<Header topics={topicsState} />
<Switch>
<Route path=/ exact>
<Dashboard topics={topicsState} />
</Route>
<Route
path=/language/:languagename
exact
render={handleRepositoryPages()}
/>

<Redirect to=/ />
</Switch>
</Router>
</>
);
};

More From » reactjs

 Answers
34

You can only use useParams in a component that is a child of your Router component, but App is the parent in your case.



The Router component injects the context containing the match into the tree below it which will be read by the useParams hook by internally using the useContext hook.


[#51410] Monday, November 25, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryantc

Total Points: 455
Total Questions: 96
Total Answers: 110

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
bryantc questions
Fri, Aug 13, 21, 00:00, 3 Years ago
Tue, Mar 30, 21, 00:00, 3 Years ago
Fri, Jun 5, 20, 00:00, 4 Years ago
Wed, May 27, 20, 00:00, 4 Years ago
;