简体   繁体   English

为什么我无法渲染标题?

[英]Why am I not able to render my header?

I'm trying to make Tech Stack appear on the top of the iOS simulator. 我试图使Tech Stack出现在iOS模拟器的顶部。 Instead, I'm getting a weird error on both my .js files even though they seem syntactically correct. 相反,我的两个.js文件都收到了一个奇怪的错误,即使它们在语法上似乎正确。

The error says: unexpected block statement surrounding arrow body arrow-body-style on both .js files. 错误消息:两个.js文件上的unexpected block statement surrounding arrow body arrow-body-style

I came across this website https://eslint.org/docs/rules/arrow-body-style to get a little more info as well as search SO thoroughly for a solution but couldn't find anything. 我碰到了这个网站https://eslint.org/docs/rules/arrow-body-style,以获取更多信息以及对SO进行彻底搜索以寻求解决方案,但找不到任何东西。

(the location of the error is commented in both .js files) (在两个.js文件中注释了错误的位置)

How can I rectify this issue? 如何解决此问题?

Error in simulator: 模拟器错误:

在此处输入图片说明

Here's app.js : 这是app.js

import React from 'react';
import { View } from 'react-native';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducers from './reducers';
import { Header } from './components/common';

const App = () => { // error is on this line
  return (
    <Provider store={createStore(reducers)}>
      <View>
        <Header headerText="Tech Stack" />
      </View>
    </Provider>
  );
};

export default App;

Here's CardSection.js 这是CardSection.js

import React from 'react';
import { View } from 'react-native';

const CardSection = (props) => { // error is on this line
  return (
    <View style={styles.containerStyle}>
      {props.children}
    </View>
  );
};

const styles = {
  containerStyle: {
    borderBottomWidth: 1,
    padding: 5,
    backgroundColor: '#fff',
    justifyContent: 'flex-start',
    flexDirection: 'row',
    borderColor: '#ddd',
    position: 'relative'
  }
};

export { CardSection };

It means that block body is not required use concise body, like this: 这意味着不需要块主体使用简洁的主体,如下所示:

const App = () => (    // not {, use (
    <Provider store={createStore(reducers)}>
        <View>
            <Header headerText="Tech Stack" />
        </View>
    </Provider>
);

There are two ways of using arrow function [MDN Doc] : 使用箭头功能[MDN Doc]有两种方法:

1- Block Body: When using {} and explicitly return is required inside the body, it is useful when you have some js logic like doing some calculation, creating function/variables. 1-块主体:当使用{}并在主体内部需要显式返回时,当您具有一些js逻辑(例如进行一些计算,创建函数/变量)时,这很有用。

Example: 例:

const Hello = () => {
    const name = 'ABC';
    // other logic
    return (<div> Hello {name} </div>);
}

2- Concise body: When you want to return something directly, use () and return will be not required. 2-简洁的主体:当您想直接返回某些内容时,请使用()并且不需要返回。

Seems like you have eslint enabled for your project. 好像您已经为项目启用了eslint To get rid of this eslint error you should wrap your App component into brackets like the following 为了摆脱这种的eslint错误,你应该换你的App组件到像下面的括号

const App = () => (
  <Provider store={createStore(reducers)}>
    <View>
      <Header headerText="Tech Stack" />
    </View>
  </Provider>
)

Curly braces is unnecessary in this case. 在这种情况下,不需要大括号。

Hope it will help. 希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM