2

我正在尝试获取coingecko-api比特币的实时价格。我正在尝试将 getServerSideProps 的返回道具传递给我的<CalculatorBuy />组件,该组件是组件的一部分<Main />。我试图导入异步函数,calculatorbuy.js但我得到了未定义的对象。如何从index.jstomain.jscalculatorbuy.jscomponents 传递 props。在 index.js 中,一切都像魅力一样工作,但我想直接在组件中使用 props 值。

index.js

export default function Home(props) {

  const {data} = props.result;
  console.log(data);

  return (
    <div className="container">
      <Head>
        <title>Buy BTC</title>
        <link rel="icon" href="/favicon.ico" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
      </Head>
      <Header />

      <Main />

      <Footer />
      <style jsx> {`
        .container {
          min-height: 100vh;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
        }
      `} </style>
    </div>
  )
}

export async function getServerSideProps(context) {
  const result = await coinGeckoClient.simple.price({
      ids: "bitcoin",
      vs_currencies: "eur",
  });
  return {
      props: {
          result,
      },
  }
  
}
main.js

import React, { useState } from 'react';
import Button from '@material-ui/core/Button';
import Calculatorbuy from './calculatorbuy.js'
import Calculatorsell from './calculatorsell.js'
  

export default function Main() {
    const [ showMe, setShowMe ] = useState(true);
    function toggle (){
        if  (!showMe) {
            setShowMe(true);
        }
        else {
            setShowMe(true);
        }
    }
    function toggle2 (){
        if  (showMe) {
            setShowMe(false);
        }
        else {
            setShowMe(false);
        }
    }
    
    return (
        <main className="main">
            <div className="box">
                <div className="buttons">

                    <Button onClick={toggle} variant="outlined" color="primary" style={{width: 120, marginRight: 10}}>
                        KUP
                    </Button>
                    <Button onClick={toggle2} variant="outlined" color="secondary" style={{width: 120, marginRight: 10}}>
                        SPRZEDAJ
                    </Button>
                    <Button variant="outlined" color="default" style={{width: 120,}}>
                        HISTORIA
                    </Button>
                </div>
                <div style={{ display: showMe?"block":"none" }}>
                    <Calculatorbuy />
                </div>
                <div style={{ display: !showMe?"block":"none" }}>
                    <Calculatorsell />
                </div>
            </div>
            <div className="room-for-socials"></div>
import React, { useState } from 'react';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import Livebsv from './livebsv.js';



export default function Calculatorbuy() {
    const [value, setValue] = useState(0);
   
    return (
        <form className="calculator" noValidate autoComplete="off">
            <div>
                <Livebsv />
            </div>
            <div className="typebox">
                <div className="textfield">
                    <TextField error={false} id="outlined-number" label="PLN" helperText="Min. wartość 100zł"  
                    type="tel"
                    value={value}
                    InputProps={{ 
                        inputProps: { min: "100", max: "5000", step: "0.01" } 
                    }}
                    variant="outlined"
                    onKeyPress={(e) => {
                        if (!/[0-9]/.test(e.key)) {
                          e.preventDefault();
                        }
                      }}
                    onChange={(e) => setValue(e.currentTarget.value)}
                    onKeyPress={(e) => {
                        if (!/[0-9]/.test(e.key)) {
                          e.preventDefault();
                        }
                      }}
                    onBlur={(e) => {
                      if (e.currentTarget.value > 0 & e.currentTarget.value < 100 ) 
                        setValue(100);
                      else if (e.currentTarget.value > 5000) 
                        setValue(5000);
                    }}
                    />
                </div>
                <div className="textfield">
                    <TextField disabled id="outlined-disabled" value={(value).toFixed(8)} label="BSV" variant="outlined" 


4

1 回答 1

6

通过在 index.js(getServerSideProps) 上加载结果,您开始做得很好。

然后,要将数据传递给 Main,您必须将其添加为组件的属性:

<Main data={data} />

现在,由于 Main 需要一个参数,它必须在 main.js 中定义:

export default function Main(props) {
    const data = props.data;
    ...
}

然后,对于 Calculatorbuy 组件,您必须在 Main 上执行相同的操作。定义道具并使用它。

于 2021-04-04T22:09:36.457 回答