最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I configure create-react-app to create an app with classes and not functions? - Stack Overflow

programmeradmin2浏览0评论

I am following a YouTube tutorial in order to learn React, but I noticed something annoying. When I create a new app with create-react-app, the app.js is using a function App(), and not a class. I know React can be coded both ways, but I would like to work with classes. Is there any way I can configure create-react-app to create a React app with classes? Something like adding an argument on the CLI, or maybe change an option on a setup page (I couldn't find any).

function App() {}

What I want:

class App extends Component {}

I am following a YouTube tutorial in order to learn React, but I noticed something annoying. When I create a new app with create-react-app, the app.js is using a function App(), and not a class. I know React can be coded both ways, but I would like to work with classes. Is there any way I can configure create-react-app to create a React app with classes? Something like adding an argument on the CLI, or maybe change an option on a setup page (I couldn't find any).

function App() {}

What I want:

class App extends Component {}
Share edited Apr 17, 2020 at 8:18 Penny Liu 17.5k5 gold badges86 silver badges108 bronze badges asked Apr 24, 2019 at 14:11 Jeremy SJeremy S 472 silver badges7 bronze badges 4
  • 2 Why don't just change it to class? – Dimitar Tsonev Commented Apr 24, 2019 at 14:18
  • You aren't locked in to either class or function you can mix and match, so you can change the app function to a class and every new ponent you create you can create as class. – xDreamCoding Commented Apr 24, 2019 at 14:41
  • I understand what both of you say, and I am perfectly aware that I can mix both notations. that was not my question :) I am looking for a way to make create-react-app to do it from start! – Jeremy S Commented Apr 24, 2019 at 15:24
  • What you want to achieve is anti-pattern, as not all of your ponents need state nor need to be classes. If you’re looking for shorthand mands to create a boilerplate ponent class, then look into “snippets” for your IDE. – Matt Carlotta Commented Apr 24, 2019 at 17:04
Add a ment  | 

3 Answers 3

Reset to default 6

If you would like your create-react-app to generate class instead of a function then you need to use the following mand to create App with the older script version.

create-react-app my-app --scripts-version react-scripts@^2

What you are observing is not adjustable by configuration. They changed the App.js generation in create-react-app from class to function with mit e4fdac2 and mit b424737. You are using a version of create-react-app more recent than the videos you are watching. Either syntax is valid, depending on whether you need state or not, but you will have to alter the code yourself to use the former class syntax in App.js.

Actually there is a solution to your problem. You just need an older version of react-scripts and your project configuration will fall back to using classes. When creating your project use this mand:

create-react-app [Project Name] --scripts-version 2.0.3
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>