gpt4 book ai didi

javascript - Next.js 使用 getServerSideProps 如何将 Prop 从页面传递到组件?

转载 作者:行者123 更新时间:2023-12-04 07:51:34 25 4
gpt4 key购买 nike

我正在尝试获取 coingecko-api访问比特币的实时价格。我试图将 getServerSideProps 的返回 Prop 传递给我的 <CalculatorBuy />组件是 <Main /> 的一部分成分。我试图在 calculatorbuy.js 中导入异步函数但我得到了 undefined object 。如何从index.js传递 Prop 至 main.jscalculatorbuy.js成分。在 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"


最佳答案

通过在 index.js(getServerSideProps) 上加载结果,您开始得很好。
然后,要将数据传递给 Main,您必须将其添加为组件的属性:

<Main data={data} />
现在,因为 Main 需要一个参数,它必须在 main.js 中定义:
export default function Main(props) {
const data = props.data;
...
}
然后,对于 Calculatorbuy 组件,您必须像在 Main 上一样执行相同的操作。定义 Prop 并使用它。

关于javascript - Next.js 使用 getServerSideProps 如何将 Prop 从页面传递到组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66945555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com