how can i pass data from a function to another file in react with calling that component or use props

huangapple go评论86阅读模式
英文:

how can i pass data from a function to another file in react with calling that component or use props

问题

  1. filename - Header.jsx
  2. import { useEffect, useState } from 'react';
  3. import { getValue } from './Input';
  4. import TextField from '@mui/material/TextField';
  5. export const Header = () => {
  6. const [search, setSearch] = useState('');
  7. useEffect(() => {
  8. getValue(search, continents);
  9. }, [search, continents]);
  10. return (
  11. <TextField id="outlined-basic" label="Outlined" variant="outlined" onChange={(e) => setSearch(e.target.value)} />
  12. );
  13. }
  14. filename - CountryData.jsx
  15. let setVal = [];
  16. export const getValue = (...val) => {
  17. setVal = val;
  18. return setVal;
  19. };
  20. export const CountryData = () => {
  21. const [searchVal, setSearchVal] = useState('');
  22. useEffect(() => {
  23. setSearchVal(setVal[0]);
  24. }, [setVal, getValue]);
  25. return null; // This component doesn't render anything, as you want to separate the rendering.
  26. }

i want to pass the data from input field from Header.jsx file to CountryData.jsx file without calling the component because i want to separate the both header and CountryData element separate in html. Maybe i am wrong in doing something. Please help and Thank you.

英文:
  1. filename - Header.jsx
  2. import { useEffect, useState } from &#39;react&#39;
  3. import { getValue } from &#39;./Input&#39;
  4. import TextField from &#39;@mui/material/TextField&#39;;
  5. export const Header = () =&gt; {
  6. const [search, setSearch] = useState(&#39;&#39;)
  7. useEffect(()=&gt;{
  8. getValue(search,continents)
  9. },[search,continents])
  10. &lt;TextField id=&quot;outlined-basic&quot; label=&quot;Outlined&quot; variant=&quot;outlined&quot; onChange={(e) =&gt; setSearch(e.target.value)} /&gt;`
  11. filename - CountryData.jsx
  12. let setVal = [];
  13. export const getValue = (...val) =&gt; {
  14. setVal = val;
  15. return setVal;
  16. };
  17. export const CountryData = () =&gt; {
  18. const [searchVal, setSearchVal] = useState(&#39;&#39;)
  19. useEffect(() =&gt; {
  20. setSearchVal(setVal[0]);
  21. }, [setVal,getValue]);
  22. }

i want to pass the data from input field from Header.jsx file to CountryData.jsx file without calling the component because i want to seperate the both header and CountryData element seperate in html. Maybe i am wrong in doing something. Please help and Thankyou

答案1

得分: 0

你可以使用useContext。将其想象成你的应用程序的存储库。在你的 Header 组件中,你可以将数据/值设置到该存储库中。然后,在 CountryData 组件中,你可以从存储库中检索数据以供使用。

英文:

You can use useContext. Imagine it as a store for your app. In your Header component, you can set the data/value to that store. Then, in the CountryData component, you can retrieve the data from the store to use.

huangapple
  • 本文由 发表于 2023年6月8日 11:08:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定