我正在使用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个值作为prop,一个数组存储库和一个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>
    )
  }


安慰:

回购价值:
对象数组

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

selectedLanguage的值:
欧盟

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

当在LoginForm函数中调用两个值时,我的问题来了,回购的值变成了单个字符串,而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>
  )
}


安慰:

回购价值:
欧盟

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

selectedLanguage的值:
未定义

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


为什么要转换值?
如何不通过转换就通过它们?

最佳答案

如我们所见,LoginForm需要2个道具,reposselectedLanguage

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


但是您只通过了repos

<LoginForm repos={repos, selectedLanguage} />


在这里,selectedLanguage绑定到repos道具,并且您在控制台中获取EU,而selectedLanguage道具未通过且您得到了undefined

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

您需要分别传递两个值,

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

07-24 09:50