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

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

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

问题

filename - Header.jsx

import { useEffect, useState } from 'react';
import { getValue } from './Input';
import TextField from '@mui/material/TextField';

export const Header = () => {
    const [search, setSearch] = useState('');

    useEffect(() => {
        getValue(search, continents);
    }, [search, continents]);

    return (
        <TextField id="outlined-basic" label="Outlined" variant="outlined" onChange={(e) => setSearch(e.target.value)} />
    );
}

filename - CountryData.jsx

let setVal = [];

export const getValue = (...val) => {
    setVal = val;
    return setVal;
};

export const CountryData = () => {
    const [searchVal, setSearchVal] = useState('');

    useEffect(() => {
        setSearchVal(setVal[0]);
    }, [setVal, getValue]);

    return null; // This component doesn't render anything, as you want to separate the rendering.
}

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.

英文:

filename - Header.jsx

import { useEffect, useState } from &#39;react&#39;
import { getValue } from &#39;./Input&#39;
import TextField from &#39;@mui/material/TextField&#39;;

export const Header = () =&gt; {
    const [search, setSearch] = useState(&#39;&#39;)
    useEffect(()=&gt;{
        getValue(search,continents)
    },[search,continents])

&lt;TextField id=&quot;outlined-basic&quot; label=&quot;Outlined&quot; variant=&quot;outlined&quot; onChange={(e) =&gt; setSearch(e.target.value)} /&gt;`



filename - CountryData.jsx




let setVal = [];

export const getValue = (...val) =&gt; {
  setVal = val;
  return setVal;
};
export const CountryData = () =&gt; {
const [searchVal, setSearchVal] = useState(&#39;&#39;)
useEffect(() =&gt; {
        setSearchVal(setVal[0]);
      }, [setVal,getValue]);
}

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:

确定