【问题标题】:How to avoid jsx styling error in react native?react native如何避免jsx样式错误?
【发布时间】:2019-03-18 08:53:47
【问题描述】:

每当我尝试从头开始构建 UI 时,都会收到此错误 adjacent jsx element must be wrapped in an enclosing tag。我不知道如何解决这个问题。因为我尝试了不同的方法,所以我尝试将块放在View 组件中,flex:1 但未使用。是否有任何适当的解决方案。这对我来说是一个巨大的挑战,因为我无法设计自己的任何组件。该怎么办请帮帮我。以下是我的代码。

screen.js

   export default  class FirstScreen extends Component {
  constructor(props){
super(props);
this.state = {
 showPopupDialog: false,
 workType: "",
 workers: []
}
}
      componentWillMount(){
            fetch('http://192.168.1.6:3000/api/worker', {
              method:'GET',
              headers:{
                Accept: 'application/json'
              }
            })
            .then(response => response.json())
            .then(responseData =>
              this.setState({
              workers:responseData
              })
            )
      }
      onPressYes = (workType) => {
       console.log(workType);
      }

popupDialog = (id, workType) => {
  this.setState ({
     showPopupDialog: true,
     workType: workType
  });
  //make sure you set showPopupDialog to false and workType to "" when you click yes or no button in PopupDialog component so that it will work the next time you click on card
}
render() {
    const { workers, workType, showPopupDialog} = this.state;
    return (
      <View style={{flex:1}}>
          <Header />
             <ScrollView>
                {workers.map((a, index)=> (
                   <View style={{flex:1}}>
                      <CardSection>
                        <TouchableOpacity onPress={() => this.popupDialog(a.id, a.work_type)}>
                          <View style={{ maringTop: 10, marginLeft:120}}>
                            <Image style={{ height: 100, width: 100 }} source={{ uri: a.work_type == 'Carpenter' ? images[0].image : images[1].image}}/>
                            <Text style={{marginLeft:20, fontSize:20}}>{a.work_type}</Text>
                          </View>
                        </TouchableOpacity>
                      </CardSection>
                      </View>
                  ))}
                  {showPopupDialog && <PopupDialog
                        dialogStyle={{ backgroundColor: "#FFFFFF", height: 180, width:300, borderWidth:1,padding:10}}
                        overlayBackgroundColor="#fff" dismissOnTouchOutside={true}>
                          <View style={styles.dialogContentView}>
                            <Text style={{fontSize:18, margingTop:10,color:"#000000"}}>Are you sure you want to submit?</Text>
                            <View style={{flexDirection:'row'}}>
                              <View style={styles.button_1}>
                                <Button title="Yes" color="#FF6633" onPress={() => this.onPressYes(workType)}/>
                              </View>
                              <View style={styles.button_1}>
                                <Button title="No" color="#FF6633" onPress={() =>this._onPressNo() }/>
                              </View>
                            </View>
                          </View>
                        </PopupDialog>}
                </ScrollView>
              </View>
            );
          }
        }

我面临的问题是我无法将 &lt;PopupDialog&gt; 组件与 &lt;CardSection&gt; 相邻,为此我将 &lt;PopupDialog&gt; 放在 &lt;View&gt; 中,即使它不能解决我的问题.请帮忙..请

【问题讨论】:

    标签: javascript reactjs react-native jsx


    【解决方案1】:

    如果我正确理解您的问题... 您可以通过将 is 包装在 &lt;React.Fragment&gt; 元素中来返回 jsx 中的多个根元素(您可以在 v16.2 及更高版本中使用 &lt;&gt;&lt;/&gt;)。片段是 React v16 中的新功能。在此之前,您只需将它们包装在某个元素中(通常是 divspan)。

    【讨论】:

    • 那么我可以将其用作反应原生component 吗?而不是使用View 组件?
    • 她的代码更像是 react-native。 div 或 span 对此有何帮助?
    • @anu 它基本上是一个不使用任何样式渲染的 View 组件(不是真的,但你可以这样想)
    【解决方案2】:

    试试下面更正的代码。

    有两点需要改正

    1. 您正在执行 .map 但您没有返回我拥有的任何内容 在下面的代码中更正
    export default  class FirstScreen extends Component {
      constructor(props){
    super(props);
    this.state = {
     workType: "",
     workers: []
    }
    }
          componentWillMount(){
                fetch('http://192.168.1.6:3000/api/worker', {
                  method:'GET',
                  headers:{
                    Accept: 'application/json'
                  }
                })
                .then(response => response.json())
                .then(responseData =>
                  this.setState({
                  workers:responseData
                  })
                )
          }
          onPressYes = (workType) => {
           console.log(workType);
          }
    
    popUpDialog = (id, workType) => {
      this.setState ({
         workType: workType
      });
      this.popupDialog.show();
    
      //make sure you set workType to "" when you click yes or no button in PopupDialog component so that it will work the next time you click on card
    }
    render() {
        const { workers, workType} = this.state;
        return (
          <View style={{flex:1}}>
              <Header />
                 <ScrollView>
                    {workers.map((a, index)=> (
                       <View style={{flex:1}}>
                          <CardSection>
                            <TouchableOpacity onPress={() => this.popUpDialog(a.id, a.work_type)}>
                              <View style={{ maringTop: 10, marginLeft:120}}>
                                <Image style={{ height: 100, width: 100 }} source={{ uri: a.work_type == 'Carpenter' ? images[0].image : images[1].image}}/>
                                <Text style={{marginLeft:20, fontSize:20}}>{a.work_type}</Text>
                              </View>
                            </TouchableOpacity>
                          </CardSection>
                          </View>
                      ))}
                      <PopupDialog ref={popupDialog => {
                               this.popupDialog = popupDialog;
                             }}
                            dialogStyle={{ backgroundColor: "#FFFFFF", height: 180, width:300, borderWidth:1,padding:10}}
                            overlayBackgroundColor="#fff" dismissOnTouchOutside={true}>
                              <View style={styles.dialogContentView}>
                                <Text style={{fontSize:18, margingTop:10,color:"#000000"}}>Are you sure you want to submit?</Text>
                                <View style={{flexDirection:'row'}}>
                                  <View style={styles.button_1}>
                                    <Button title="Yes" color="#FF6633" onPress={() => this.onPressYes(workType)}/>
                                  </View>
                                  <View style={styles.button_1}>
                                    <Button title="No" color="#FF6633" onPress={() =>this._onPressNo() }/>
                                  </View>
                                </View>
                              </View>
                            </PopupDialog>
                    </ScrollView>
                  </View>
                );
              }
            }
    

    【讨论】:

      【解决方案3】:

      问题是你有这个结构:

      <a>
        {this.state.workers.map((a, index)=>
          <b/>
          <c/>
        )}
      </a>
      

      由于 &lt;b/&gt;&lt;c/&gt; 被单独解析并且没有封闭元素,因此您会收到错误消息。但最终结构不需要封闭元素,确实有一个封闭元素。解决方案是简单地返回一个 JSX 元素数组,如下所示:

      <a>
        {this.state.workers.map((a, index)=>
          [<b/>,
           <c/>]
        )}
      </a>
      

      【讨论】:

      • 你的回答和我 19 分钟前回答的一样。我对你的答案投了反对票吗?没有。
      • @Think-Twice 我使用的是 Array,而不是额外的 View。我现在已经多次解释了我投反对票的原因。
      • @ChrisG 对不起,我没有时间和你解释或争论
      猜你喜欢
      • 2021-10-03
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      相关资源
      最近更新 更多