搜尋

首頁  >  問答  >  主體

如何從元件取得變數以放在 App.js 上

即使在網路上搜尋我也沒有找到,或者說我不明白。 我的問題:我希望在“InputField.js”元件中找到“inputVal”變量,其中有“!!HERE!!”在“App.js”元件中(在獲取時) 請幫我 感謝您閱讀我的留言!

export default function InputField() {
  
  function handleSubmit(e) {
    // Prevent the browser from reloading the page
    e.preventDefault();

    // Read the form data
    const form = e.target;
    const inputVal = form.myInput.value;

    console.log(inputVal);
  }

  return (
    <form method="post" onSubmit={handleSubmit}>
      <input name="myInput" id="adress-field" placeholder="Enter adress" autoComplete="on" />
      <button type="submit" id="adress-button">Send</button>
    </form>
  );
}
import './App.css';
import AccountNumber from "./components/AccountNumber";
import InputField from "./components/InputField";
import { useEffect, useState } from "react"

function App() {

  //token fetch
  const [tockens, setTockens] = useState([])
  const [loading, setLoading] = useState(false)
  useEffect(() => {
    setLoading(true)
    fetch("https://api.multiversx.com/accounts/!!HERE!!/tokens")
      .then(response => response.json())
      .then(json => setTockens(json))
      .finally(() => {
        setLoading(false)
      })
      console.log(tockens); 
  }, [])


  function round(nr, ten) { // arondi un chiffre.
    return Math.round(nr * ten) / ten;
}

function numberWithSpaces(nr) { // formate un chiffre(x xxx xxx).
    return nr.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}



  return (
    <content className="content">
        <div className="up-side">
            <div className="account-number-box">
                <p id="p-account-number">Total number of accounts</p>
                <p id="account-number"><AccountNumber/></p>
            </div>
            <div className="adress-search">
                {InputField()}
            </div>
            <p>{window.inputVal}</p>
        </div>
        <div className="down-side">
            <table className="Token-section-output">
            

              {loading ? (
                <div>Loading...</div>
              ) : (
                <>
                  <h1>Tockens</h1>
                  <table className='Token-section-output' border={0}>
                    <tr className='token-row-type'>
                      <th className='token-column'>Name</th>
                      <th className='center-column'>Price</th>
                      <th>Hold</th>
                    </tr>
                    <tr className="space20"/>

                    
                    {tockens.map(tocken => (
                      <tr className='token-row' key={tocken.id}>
                        <td className='token-column'>
                        <img className="img-Tockens" src = {tocken?.assets?.pngUrl ?? "img/Question.png"} /> 
                          <p>{tocken.name}</p> 
                        </td>

                        <td className='center-column'> <p>${round(tocken.price, 10000000)}</p> </td>

                        <td> 
                          <p>{round(tocken.balance / Math.pow(10, tocken.decimals), 10000000)}</p> 
                          <p className='token-hold'>${round(tocken.valueUsd, 10000000)}</p>
                        </td>
                      </tr>
                    ))}

                  </table>
                </>
              )}


            </table>
        </div>   
    </content>
  );
}

export default App;

我的反應不太好,我在網路上搜尋

P粉250422045P粉250422045437 天前459

全部回覆(1)我來回復

  • P粉668019339

    P粉6680193392023-09-15 12:28:59

    您想要擴充您的 InputField 元件以接受可由您的應用程式傳遞的回呼函數:

    export default function InputField({onSubmit}) {
      
      function handleSubmit(e) {
        // Prevent the browser from reloading the page
        e.preventDefault();
    
        // Read the form data
        const form = e.target;
        const inputVal = form.myInput.value;
    
        console.log(inputVal);
        onSubmit(inputVal)
      }
    
      ...
    }

    在您的應用程式中,您需要將該回調傳遞給您的元件:

    <div className="adress-search">
      <InputField onSubmit={handleSearchSubmit} />
    </div>

    注意:元件不會像函數一樣透過呼叫而被消耗。

    在您的應用程式邏輯中,您需要另一個狀態來保存您的搜尋值:

    ...
    const [searchValue, setSearchValue] = useState(null);
    
    const handleSearchSubmit = (val) => {
      setSearchValue(val);
    }
    
    useEffect(() => {
        setLoading(true)
        fetch(`https://api.multiversx.com/accounts/${searchValue}/tokens`)
          .then(response => response.json())
          .then(json => setTockens(json))
          .finally(() => {
            setLoading(false)
          });
        console.log(tockens); 
    }, [searchValue])
    ...

    回覆
    0
  • 取消回覆