代码之家  ›  专栏  ›  技术社区  ›  DCR

为什么这是一次退换货,而不是另一次退换货

  •  0
  • DCR  · 技术社区  · 7 年前

    在类应用程序的呈现函数中,我有:

      render() {
    
      return(
        {this.state.showForm &&
            <AddContactForm
              addContact={this.addContact}>
            </AddContactForm>}
      )
    

    它回来说:“这是一个保留字。如果我将“this”改为:

      var showForm = this.state.showForm
      return(
    
        { showForm &&
            <AddContactForm
              addContact={this.addContact}/>
          }
      )
    

    我得到了意想不到的纪念品。

    但在以下代码中,结构工作正常:

    {this.state.show && this.state.selectedIndex === 2 &&
          <ContactsList
            contacts={this.state.contacts}>
          </ContactsList>}
    

    为什么这在第一个示例中不起作用,但在最后一个示例中起作用?

    //app.js
    
    import React from 'react';
    import { StyleSheet, Text, ScrollView, FlatList, SectionList, View, Button,
             SegmentedControlIOS,TextInput } from 'react-native';
    import contacts, {compareNames} from './contacts';
    import {Constants} from 'expo';
    import PropTypes from 'prop-types'
    
    export class AddContactForm extends React.Component {
    //  static propTypes ={
    //    addContact: PropTypes.func,
    //  }
    
      state={
        name:'',
        phone:'',
      }
    
      handleName=(name)=>{
        this.setState({name:name})
      }
    
      handlePhone=(phone)=>{
        this.setState({phone:phone})
      }
    
      onSubmit=()=>{
        this.props.addContact(this.state.name,this.state.phone)
        this.setState({name:'',phone:''})
      }
    
      render() {
        return(
          <View style={{paddingTop:20}}>
            <TextInput
              style={styles.input}
              value={this.state.name}
              placeholder='name'
              onChangeText={this.handleName}/>
            <TextInput
              style={styles.input}
              value={this.state.phone}
              placeholder='phone'
              keyboardType='numeric'
              onChangeText={this.handlePhone} />
            <Button title='Add Contact' onPress={this.onSubmit} />
          </View>
        )
      }
    }
    
    function getRandomColor() {
      var letters = '0123456789ABCDEF';
      var color = '#';
      for (var i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
      }
      return color;
    }
    
    var x = 0;
    const Row=(props)=>(
      <View style={styles.row}>
        <Text style={{color:props.color}} >{props.name}</Text>
        <Text >{props.phone}</Text>
      </View>
    )
    
    const renderItem=(obj)=> {
        //  console.log('render',x++)
    
         return(<Row  {...(obj.item)} color={getRandomColor()} />)
    
    
    }
    
    const ContactsList = props => {
      const renderSectionHeader=(obj) =><Text>{obj.section.title}</Text>
        const contactsByLetter = props.contacts.reduce((obj, contact) =>{
         const firstLetter = contact.name[0].toUpperCase()
         return{
           ...obj,
           [firstLetter]: [...(obj[firstLetter] || []),contact],
         }
       },{})
    
       const sections = Object.keys(contactsByLetter).sort().map(letter=>({
          title: letter,
          data: contactsByLetter[letter],
       }))
    
      return(
      <SectionList
        keyExtractor = { (item, key) => key.toString() }
        renderItem={renderItem}
        renderSectionHeader={renderSectionHeader}
        sections={sections}
      />
    )}
    
    ContactsList.propTypes ={
      renderItem: PropTypes.func,
      renderSectionHeader: PropTypes.func,
      contacts: PropTypes.array,
      sections: PropTypes.func
    }
    
    export default class App extends React.Component {
      state={show: false, selectedIndex: 0, contacts: contacts, showForm:false}
    
      toggleContacts=()=>{
        this.setState(prevState =>({show:!prevState.show}))
      }
    
      toggleForm=()=>{
        console.log('toggleForm',this.state)
        this.setState(prevState =>({showForm:!prevState.showForm}))
      }
    
      sort=()=>{
        this.setState({contacts: [...this.state.contacts].sort(compareNames)})
      }
    
      addContact=(name,phone)=>{
        console.log('ADD',name,phone,'ADD')
      }
    
      render() {//console.log(this.state)
      //  if (this.state.showForm===true) {
    
    
          return(
            {this.state.showForm &&
                <AddContactForm
                  addContact={this.addContact}>
                </AddContactForm>}
          )
      //  }
    
        return (
          <View style={styles.container}>
            <Button title="toggle names" onPress={this.toggleContacts}  />
            <Button title="sort" onPress={this.sort}  />
            <Button title="add contact" onPress={this.toggleForm}  />
            <SegmentedControlIOS
              values={['ScrollView', 'FlatList','SectionList']}
              selectedIndex={this.state.selectedIndex}
              onChange={(event) => {
                this.setState({selectedIndex: event.nativeEvent.selectedSegmentIndex});
              }} />
            {this.state.show && this.state.selectedIndex === 0 &&
              <ScrollView >
                {this.state.contacts.map(contact=>(
                  <Row  {...contact}/> ))}
              </ScrollView>}
            {this.state.show && this.state.selectedIndex === 1 &&
              <FlatList
                data={this.state.contacts}
                keyExtractor = { (item, index) => index.toString() }
                renderItem={renderItem}>
              </FlatList>}
            {this.state.show && this.state.selectedIndex === 2 &&
              <ContactsList
                contacts={this.state.contacts}>
              </ContactsList>}
          </View>
        )
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: '#fff',
        // alignItems: 'flex-start',
        paddingTop: Constants.statusBarHeight + 25,
      },
      row: {
        padding:20,
      },
      input: {
        padding:5,
    
        borderColor:'black',
        borderWidth:1,
      },
    });
    //contacts.js
    
    const NUM_CONTACTS = 4
    
    const firstNames = ['Emma','Noah','Olivia','Liam','Ava','William','Sophia','Mason','Isabella','James','Mia','Benjamin','Charlotte','Jacob','Abigail','Michael','Emily','Elijah','Harper','Ethan','Amelia','Alexander','Evelyn','Oliver','Elizabeth','Daniel','Sofia','Lucas','Madison','Matthew','Avery','Aiden','Ella','Jackson','Scarlett','Logan','Grace','David','Chloe','Joseph','Victoria','Samuel','Riley','Henry','Aria','Owen','Lily','Sebastian','Aubrey','Gabriel','Zoey','Carter','Penelope','Jayden','Lillian','John','Addison','Luke','Layla','Anthony','Natalie','Isaac','Camila','Dylan','Hannah','Wyatt','Brooklyn','Andrew','Zoe','Joshua','Nora','Christopher','Leah','Grayson','Savannah','Jack','Audrey','Julian','Claire','Ryan','Eleanor','Jaxon','Skylar','Levi','Ellie','Nathan','Samantha','Caleb','Stella','Hunter','Paisley','Christian','Violet','Isaiah','Mila','Thomas','Allison','Aaron','Alexa','Lincoln']
    
    const lastNames = ['Smith','Jones','Brown','Johnson','Williams','Miller','Taylor','Wilson','Davis','White','Clark','Hall','Thomas','Thompson','Moore','Hill','Walker','Anderson','Wright','Martin','Wood','Allen','Robinson','Lewis','Scott','Young','Jackson','Adams','Tryniski','Green','Evans','King','Baker','John','Harris','Roberts','Campbell','James','Stewart','Lee','County','Turner','Parker','Cook','Mc','Edwards','Morris','Mitchell','Bell','Ward','Watson','Morgan','Davies','Cooper','Phillips','Rogers','Gray','Hughes','Harrison','Carter','Murphy']
    
    // generate a random number between min and max
    const rand = (max, min = 0) => Math.floor(Math.random() * (max - min + 1)) + min
    
    // generate a name
    const generateName = () => `${firstNames[rand(firstNames.length - 1)]} ${lastNames[rand(lastNames.length - 1)]}`
    
    // generate a phone number
    const generatePhoneNumber = () => `${rand(999, 100)}-${rand(999, 100)}-${rand(9999, 1000)}`
    
    // create a person
    const createContact = () => ({name: generateName(), phone: generatePhoneNumber()})
    
    // compare two contacts for alphabetizing
    export const compareNames = (contact1, contact2) => contact1.name > contact2.name
    
    // add keys to based on index
    const addKeys = (val, key) => ({key, ...val})
    
    // create an array of length NUM_CONTACTS and alphabetize by name
    export default Array.from({length: NUM_CONTACTS}, createContact).map(addKeys)
    2 回复  |  直到 7 年前
        1
  •  1
  •   digit    7 年前

    由于返回语句,块被视为 object literal

    对象文本是零对或多对属性名的列表。 和对象的关联值,用大括号()括起来。做 不要在语句开头使用对象文本。这将 导致错误或行为不符合您的预期,因为 解释为块的开头。

    要解决这个问题,只需将条件包装在JSX元素中,如:

    return (
        <View>
          {this.state.showForm &&
                <AddContactForm
                  addContact={this.addContact}>
                </AddContactForm>}
       </View> 
    )
    
        2
  •  1
  •   DCR    7 年前

    我想可能是因为回信里面的花括号 return ({})

    尝试不使用大括号,例如:

    if (this.state.showForm) return <AddContactForm addContact={this.addContact}/>

    看起来这行,但解释得不好

    推荐文章