gpt4 book ai didi

javascript - 如何在 React 中将数组作为 props 传递而不转换为字符串

转载 作者:行者123 更新时间:2023-12-01 00:41:05 26 4
gpt4 key购买 nike

我正在使用 React 开发一个大学管理网站,可以用不同的语言显示。到目前为止,我已经开发了登录页面,请看代码(注意我使用的是Babel):

import React from 'react'
import PropTypes from 'prop-types'
import { fetchPopularRepos } from '../utils/api'

function LanguagesNav ({ selected, onUpdateLanguage}) {
const languages = ['EU', 'ES', 'EN']

return (
<div >
<h1 className='center-text header-lg'>
GAUR 2.0
</h1>
<ul className='flex-center'>
{languages.map((language) => (
<li key={language}>
<button
className='btn-clear nav-link'
style={language === selected ? { color: 'rgb(187, 46, 31)' } : null }
onClick={() => onUpdateLanguage(language)}>
{language}
</button>
</li>
))}
</ul>
</div>
)
}

LanguagesNav.propTypes = {
selected: PropTypes.string.isRequired,
onUpdateLanguage: PropTypes.func.isRequired
}

function LoginForm ({ repos, selectedLanguage }) {
return (
<form className='column player'>
{console.log(repos)}
{console.log(selectedLanguage)}

{(repos, selectedLanguage) => {
var lang = { username, password, login }

switch (selectedLanguage) {
case "EU":
selectedLanguage = "EU";
lang = repos;
break;
case "ES":
selectedLanguage = "ES";
lang = repos;
break;
case "EN":
selectedLanguage = "EN";
lang = repos;
break;
}

return (
<React.Fragment>
<div className='row player-inputs'>
<input
type='text'
id='username'
className='input-light'
placeholder={lang.terms.username}
autoComplete='off'
/>
</div>
<div className='row player-inputs'>
<input
type='password'
id='password'
className='input-light'
placeholder={lang.terms.password}
autoComplete='off'
/>
</div>
<div className='row player-inputs'>
<button
className='btn dark-btn'
type='submit'
>
{lang.terms.login}
</button>
</div>
</React.Fragment>
)
}}
</form>
)
}

LoginForm.propTypes = {
repos: PropTypes.array.isRequired
}

export default class Login extends React.Component {
constructor(props) {
super(props)

this.state = {
selectedLanguage: 'EU',
repos: null,
error: null
}

this.updateLanguage = this.updateLanguage.bind(this)
this.isLoading = this.isLoading.bind(this)
}
componentDidMount () {
this.updateLanguage(this.state.selectedLanguage)
}
updateLanguage (selectedLanguage) {
this.setState({
selectedLanguage,
error: null,
repos: null
})

fetchPopularRepos(selectedLanguage)
.then((repos) => this.setState({
repos,
error: null,
}))
.catch(() => {
console.warn('Error fetching repos: ', error)

this.setState({
error: 'There was an error fetching the repositories.'
})
})
}
isLoading() {
return this.state.repos === null && this.state.error === null
}
render() {
const { selectedLanguage, repos, error } = this.state

return (
<React.Fragment>
<LanguagesNav
selected={selectedLanguage}
onUpdateLanguage={this.updateLanguage}
/>

{this.isLoading() && <p>LOADING...</p>}

{error && <p>{error}</p>}

{repos != null && <LoginForm repos={repos, selectedLanguage} />}

{this.state.repos != null && console.log(`${JSON.stringify(repos)}`)}
{this.state.repos != null && console.log(`${selectedLanguage}`)}

</React.Fragment>
)
}
}

正在从此 JSON 文件检索数据:

{
"languages": [
{
"language": "EU",
"terms": {
"username": "Erabiltzailea",
"password": "Pasahitza",
"welcome": "Sartu GAUR 2.0ra",
"login": "Sartu"
}
},
{
"language": "ES",
"terms": {
"username": "Usuario",
"password": "Contraseña",
"welcome": "Entra a GAUR 2.0",
"login": "Entrar"
}
},
{
"language": "EN",
"terms": {
"password": "Password",
"username": "Username",
"welcome": "Log into GAUR 2.0",
"login": "Log in"
}
}
]
}

我传递了 2 个值作为 props,一个数组 repos 和一个字符串 selectedLanguage,因为它可以在我包含的 console.logs 的控制台中看到在代码中:

render() {
const { selectedLanguage, repos, error } = this.state

return (
<React.Fragment>
<LanguagesNav
selected={selectedLanguage}
onUpdateLanguage={this.updateLanguage}
/>

{this.isLoading() && <p>LOADING...</p>}

{error && <p>{error}</p>}

{repos != null && <LoginForm repos={repos, selectedLanguage} />}

{this.state.repos != null && console.log(`${JSON.stringify(repos)}`)}
{this.state.repos != null && console.log(`${selectedLanguage}`)}

</React.Fragment>
)
}

控制台:

存储库的值(value):对象数组

repos console value before props

selectedLanguage的值:欧盟

selectedLanguage console value before props

当调用 LoginForm 函数中的两个值时,我的问题出现了,repos 的值变成单个字符串,selectedLanguage 的值变得未定义:

登录表单

function LoginForm ({ repos, selectedLanguage }) {
return (
<form className='column player'>
{console.log(repos)}
{console.log(selectedLanguage)}
{(repos, selectedLanguage) => {
var lang = { username, password, login }

switch (selectedLanguage) {
case "EU":
selectedLanguage = "EU";
lang = repos;
break;
case "ES":
selectedLanguage = "ES";
lang = repos;
break;
case "EN":
selectedLanguage = "EN";
lang = repos;
break;
}

return (
<React.Fragment>
<div className='row player-inputs'>
<input
type='text'
id='username'
className='input-light'
placeholder={lang.terms.username}
autoComplete='off'
/>
</div>
<div className='row player-inputs'>
<input
type='password'
id='password'
className='input-light'
placeholder={lang.terms.password}
autoComplete='off'
/>
</div>
<div className='row player-inputs'>
<button
className='btn dark-btn'
type='submit'
>
{lang.terms.login}
</button>
</div>
</React.Fragment>
)
}}
</form>
)
}

控制台:

存储库的值(value):欧盟

repos console value after props

selectedLanguage的值:未定义

selectedLanguage console value after props

  • 为什么要转换值?
  • 如何才能在不转换的情况下传递它们?

最佳答案

正如我们所见,LoginForm 需要 2 个属性,reposselectedLanguage

function LoginForm ({ repos, selectedLanguage }) { ... }

但是您只传递repos

<LoginForm repos={repos, selectedLanguage} />

此处 selectedLanguage 正在绑定(bind)到 repos 属性,并且您将在控制台中获取 EU,而 selectedLanguage prop 没有被传递,并且你得到了未定义

我也很惊讶,为什么repos={repos, selectedLanguage}没有给出语法错误

您需要分别传递这两个值,

<LoginForm repos={repos} selectedLanguage={selectedLanguage} />

关于javascript - 如何在 React 中将数组作为 props 传递而不转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57705819/

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