質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

2回答

1496閲覧

TypeError: Class constructor FormApp cannot be invoked without 'new'を解決したい

k49977

総合スコア27

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2021/07/16 12:06

編集2021/07/16 22:57

前提・実現したいこと

現在Reactの勉強をしています。
flux-utilを使ってみようと思い、以下のコードを参考にして実行したところエラーになりました。

参考サイト

TypeError: Class constructor FormApp cannot be invoked without 'new'

イメージ説明

解決方法が知りたいです。もしわかる方がいればよろしくお願いいたします。

コードは以下です
https://github.com/k49977/flux-app

▼index.js

js

1// @ts-nocheck 2import React, { Component } from 'react'; 3import ReactDOM from 'react-dom'; 4import './index.css'; 5import App from './App'; 6 7import { Dispatcher } from 'flux'; 8import { ReduceStore, Container } from 'flux/utils'; 9 10// Dispatcher 11const dispatcher = new Dispatcher(); 12 13// Action 14const act = { 15 SEND: 'send' 16}; 17 18const FormAction = { 19 send(val) { // ...[6] 20 dispatcher.dispatch({ 21 type: act.SEND, 22 value: val 23 }); 24 } 25}; 26 27// Store 28class FormStore extends ReduceStore { 29 30 getInitialState() { 31 return { 32 'value': null // ...[1] 33 }; 34 } 35 36 reduce(state, action) { 37 switch (action.type) { // ...[7] 38 case act.SEND: 39 return { 40 'value': action.value // ...[8] 41 }; 42 } 43 } 44 45}; 46 47// Storeのインスタンス生成 48const formStore = new FormStore(dispatcher); 49 50// View (React Component) 51class FormApp extends Component { 52 static getStores() { 53 return [formStore]; // ...[9] 54 } 55 static calculateState(prevState) { 56 return formStore.getState(); // ...[10] 57 } 58 render() { 59 console.log(this.state); 60 return ( 61 <div> 62 <FormInput /> 63 <FormDisplay data={this.state.value} /> // ...[11] 64 </div> 65 ); 66 } 67}; 68 69class FormInput extends Component { 70 _send(e) { // ...[4] 71 e.preventDefault(); 72 // @ts-ignore 73 FormAction.send(this.refs.myInput.value.trim()); // ...[5] 74 this.refs.myInput.value = ''; 75 return; 76 } 77 render() { 78 return ( 79 <form> 80 <input type="text" ref="myInput" defaultValue="" /> /* ...[2] */ 81 <button onClick={this._send.bind(this)}>Send</button> /* ...[3] */ 82 </form> 83 ); 84 } 85}; 86 87class FormDisplay extends Component { 88 render() { 89 return ( 90 <div>{this.props.data}</div> /* ...[12] */ 91 ); 92 } 93}; 94 95// Container 96const FormAppContainer = Container.create(FormApp); 97 98 99ReactDOM.render( 100 <FormAppContainer />, 101 document.getElementById('root') 102); 103

▼package.json

json

1{ 2 "name": "flux_app", 3 "version": "0.1.0", 4 "private": true, 5 "dependencies": { 6 "@testing-library/jest-dom": "^5.11.4", 7 "@testing-library/react": "^11.1.0", 8 "@testing-library/user-event": "^12.1.10", 9 "flux": "^4.0.1", 10 "react": "^17.0.2", 11 "react-dom": "^17.0.2", 12 "react-scripts": "4.0.3", 13 "web-vitals": "^1.0.1" 14 }, 15 "scripts": { 16 "start": "react-scripts start", 17 "build": "react-scripts build", 18 "test": "react-scripts test", 19 "eject": "react-scripts eject" 20 }, 21 "eslintConfig": { 22 "extends": [ 23 "react-app", 24 "react-app/jest" 25 ] 26 }, 27 "browserslist": { 28 "production": [ 29 ">0.2%", 30 "not dead", 31 "not op_mini all" 32 ], 33 "development": [ 34 "last 1 chrome version", 35 "last 1 firefox version", 36 "last 1 safari version" 37 ] 38 }, 39 "description": "This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).", 40 "main": "index.js", 41 "devDependencies": {}, 42 "author": "", 43 "license": "ISC" 44} 45

試したこと

TypeError: Class constructor FormApp cannot be invoked without 'new'
で検索し、変換する必要があるのかと推敲。その方法を調査中

https://stackoverflow.com/questions/51860043/javascript-es6-typeerror-class-constructor-client-cannot-be-invoked-without-ne
上記の記事を見つけたが、自分の環境にどのようにしたら適用できるだろうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

そして、starter-react-fluxの環境のApp.jsに以下を記述したところ無事画面表示され、投稿のflux-utilのコードが表示できた。(下記にもリファクタ版を転載)
一旦flux-utilを使ったアプリを実行したいというゴールは達成できたので、回避策としてDONE

イメージ説明

▼App.js

js

1// @ts-nocheck 2import React, { Component } from 'react'; 3import ReactDOM from 'react-dom'; 4import { Dispatcher } from 'flux'; 5import { ReduceStore, Container } from 'flux/utils'; 6 7// Dispatcher 8const dispatcher = new Dispatcher(); 9 10// Action 11const act = { 12 SEND: 'send' 13}; 14 15const FormAction = { 16 send(val) { // ...[6] 17 dispatcher.dispatch({ 18 type: act.SEND, 19 value: val 20 }); 21 } 22}; 23 24// Store 25class FormStore extends ReduceStore { 26 27 getInitialState() { 28 return { 29 'value': null // ...[1] 30 }; 31 } 32 33 reduce(state, action) { 34 switch (action.type) { // ...[7] 35 case act.SEND: 36 return { 37 'value': action.value // ...[8] 38 }; 39 } 40 } 41 42}; 43 44// Storeのインスタンス生成 45const formStore = new FormStore(dispatcher); 46 47// View (React Component) 48class FormApp extends Component { 49 static getStores() { 50 return [formStore]; // ...[9] 51 } 52 static calculateState(prevState) { 53 return formStore.getState(); // ...[10] 54 } 55 render() { 56 console.log(this.state); 57 return ( 58 <div> 59 <FormInput /> 60 <FormDisplay data={this.state.value} /> 61 </div> 62 ); 63 } 64}; 65 66class FormInput extends Component { 67 _send(e) { // ...[4] 68 e.preventDefault(); 69 // @ts-ignore 70 FormAction.send(this.refs.myInput.value.trim()); // ...[5] 71 this.refs.myInput.value = ''; 72 return; 73 } 74 render() { 75 return ( 76 <form> 77 <input type="text" ref="myInput" defaultValue="" /> 78 <button onClick={this._send.bind(this)}>Send</button> 79 </form> 80 ); 81 } 82}; 83 84class FormDisplay extends Component { 85 render() { 86 return ( 87 <div>{this.props.data}</div> /* ...[12] */ 88 ); 89 } 90}; 91 92// Container 93const FormAppContainer = Container.create(FormApp); 94 95 96ReactDOM.render( 97 <FormAppContainer />, 98 document.getElementById('root') 99); 100

投稿2021/07/16 23:08

k49977

総合スコア27

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

React + Flux入門#使い方
このサイトのstarter-react-fluxでとりあえずfluxを使ったアプリを生成はできた。

npx starter-react-flux init npm start ```

投稿2021/07/16 23:00

k49977

総合スコア27

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問