简体   繁体   English

如何在我的组件上看到我的数组? / 反应 / Javascript

[英]How can i see my array on my component ? / React / Javascript

I have a element like:我有一个元素:

const DropdownElements = [
  {
    key: 1,
    title: "Şehir",
    placeholder: "Şehir Seçiniz",
    apiUrl: "https://api.npoint.io/995de746afde6410e3bd",
    type: "city",
    selecteditem: "",
    data : [],
  },
  {
    key: 2,
    title: "İlçe",
    placeholder: "İlçe Seçiniz",
    apiUrl: "https://api.npoint.io/fc801dbd3fc23c2c1679", // its my apis. They hold datas from json
    type: "district",
    selecteditem: "",
    data : [],
  },
]

I fetching that url in App in useEffect.我在useEffect的应用程序中获取url。

const App = () => {
     useEffect(() => {
    DropdownElements.map((x) => {
     
      fetch(x.apiUrl)
        .then((z) => z.json())
        .then((vb) => {
         x.data=vb  // If i write x.data.push(vb) i can see it on my component but its not giving pure array.
         console.log(x.data) // I can see my datas perfectly. I trying fill my data.

          
            
         
        });
      
    });
    
  }, []);

And i setting it like that:我这样设置:

 <Space>
      {DropdownElements.map((x) => {
        return (
        
          <PickerCompanent
            showSearch
            selecteditem={idhold}
            key={x.key}
            
            placeholder={x.placeholder}
            type={x.type}
            datasource={x.data}  // I gave my datasource x.data that i filled .
            onFocus={onFocus}
            onChange={z=>onChange(z)}
            onFocus={onFocus}
            onSearch={onSearch}
          ></PickerCompanent>
        );
      })}
    </Space>

But in my component when i try write like console.log(props) my datasource is empty array.但是在我的组件中,当我尝试像console.log(props)这样编写时,我的数据源是空数组。 How can i see my datas on my component?如何查看组件上的数据? I need set my array to a state in my component.我需要在我的组件中将我的数组设置为 state。

It seems like you aren't using any kind of state in your code.您的代码中似乎没有使用任何类型的 state。

const App = () => {


 const [myData, setMyData] = useState();

     useEffect(() => {
         DropdownElements.map((x) => {
     
              fetch(x.apiUrl)
        .then((z) => z.json())
        .then((vb) => {
         x.data=vb  // If i write x.data.push(vb) i can see it on my component but its not giving pure array.
         console.log(x.data) // I can see my datas perfectly. I trying fill my data.
         // in here you'll want to be adding your data to some state
       // e.g. 
       setMyData(x.data); 

          
            
         
        });
      
    });
    
  }, []);

Then within your component, use that state:然后在您的组件中,使用该 state:

datasource={myData}  

Your object is updating but not view.您的 object 正在更新但未查看。 To achieve this you need have a component state, to which we can update and trigger return again to update view.为此,您需要一个组件 state,我们可以更新并再次触发返回以更新视图。

 const App = () => { const [myData, setMyData] = useState(DropdownElements); useEffect(() => { myData.map((x, i) => { fetch(x.apiUrl).then((z) => z.json()).then((result) => { myData[i].data = result; setMyData(myData); }); }); }, []); return ( <Space> {myData.map((x) => { return ( <PickerCompanent showSearch selecteditem={idhold} key={x.key} placeholder={x.placeholder} type={x.type} datasource={x.data} // I gave my datasource x.data that i filled. onFocus={onFocus} onChange={z=>onChange(z)} onFocus={onFocus} onSearch={onSearch} ></PickerCompanent> ); })} </Space> );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM