【问题标题】:Using boolean operators with ternary operators in javascript在 javascript 中使用布尔运算符和三元运算符
【发布时间】:2021-07-14 15:39:20
【问题描述】:

NB:请有人在这里解释如何格式化我的代码,它只是不起作用,我做错了什么。

这是我根据 cmets 重构后的代码,现在可以使用了!

import React, { useState, useEffect } from 'react'
import axios from 'axios'
const App = () => {
    const [countries, setCountries] = useState([])
    useEffect(() => {
        console.log('effect');
        axios
            .get('https://restcountries.eu/rest/v2/all')
            .then(response => {
                console.log('promise fulfilled');
                const countries = response.data
                setCountries(countries)
            })
    }, [])
    const [searchVal, setSearchVal] = useState('')
    const searchOnChangeEventHandler = (event) => setSearchVal(event.target.value)
    const filteredCountries = (searchVal, countries) => countries.filter(country => country.name.toLowerCase().includes(searchVal.toLowerCase()))
    const filteredCountriesLength = filteredCountries(searchVal, countries).length
    return (
        <div>
            <h1>coutries data</h1>
            <div>
                <input
                    type='text'
                    placeholder='search countries'
                    value={searchVal}
                    onChange={searchOnChangeEventHandler}
                >
                </input>
            </div>

            <div>
                <>
                    {
                        countries.length
                            ? (searchVal
                                ? (
                                    filteredCountriesLength <= 10 && filteredCountriesLength > 1
                                        ? filteredCountries(searchVal, countries).map(country => <p key={country.name}>{country.name}</p>)
                                        : <span>{filteredCountriesLength} matches! too many matches, specify another filter...</span>
                                )
                                : <span>type to search..</span>)
                            : <span>populating countries data...</span>
                    }
                </>
            </div>
        </div >
    )
}
export default App

下面的部分现在按照我想要的方式工作,在我清理代码之后,我认为最好使用 if-else 然后按照建议转换为三进制。


        <div>
            <>
                {
                    countries.length
                        ? (searchVal
                            ? (
                                filteredCountriesLength <= 10 && filteredCountriesLength > 1
                                    ? filteredCountries(searchVal, countries).map(country => <p key={country.name}>{country.name}</p>)
                                    : <span>{filteredCountriesLength} matches! too many matches, specify another filter...</span>
                            )
                            : <span>type to search..</span>)
                        : <span>populating countries data...</span>
                }
            </>
        </div>

NB:有没有办法去除代码中的所有这些空白?

我想打印type to search whenever the searchVal input was empty

【问题讨论】:

  • 那么您面临的问题是什么?
  • 即使你让它以某种方式工作,结果也很难维护。考虑将此部分移动到单独的函数或组件中。
  • 通常我只是在普通 if(){} 中做很多 if 语句,然后我将其更改为三元
  • 代码格式不正确,我无法格式化

标签: javascript reactjs conditional-statements conditional-operator


【解决方案1】:

我不完全确定您要的是什么,但我认为通过将三元组移动到如下函数中会对您有所帮助:

const handleRender = () => {
  const length = filteredCountries(searchVal, countries)?.length

  if (length) {
    if (length <= 10) {
      return filteredCountries(searchVal, countries).map(country => <p key={country.name}>{country.name}</p>)
    } else {
      return <span>please be more specific, add filter...</span>
    }
  } 

  return <span>populating countries data...</span>
}

// render
{handleRender()}

【讨论】:

  • 以后,只需编辑您之前删除的答案并取消删除即可。
  • 当 searchVal 是 '' 时,我将如何添加 if else 以打印 'type to search'
  • @utsavojha95 它取决于什么优先,如果你想显示“类型搜索”无论如何,即使你有过滤国家,我会在if (length)上方添加一个检查并检查@987654324 @
猜你喜欢
  • 2017-04-25
  • 1970-01-01
  • 2019-04-08
  • 2011-09-27
  • 2011-03-27
  • 2013-06-28
相关资源
最近更新 更多