React Typescript - ',' expected.ts(1005) - reactjs

I'm using Typescript in an React app with Graphql.
I'm getting an error:
',' expected.ts(1005)
The only answers I find say that typescript is out of date but I'm using 3.7.2
user#3df41 ~/Desktop/34534/client (master) $ tsc -v
Version 3.7.2
user#3df41 ~/Desktop/34534/client (master) $
The error occurs here at line data.recipe.map(recipe =>
import React from 'react';
import './App.css';
import { useQuery } from 'react-apollo-hooks';
import { GET_ALL_RECIPES } from '../queries';
import { RecipeData } from '../generated/RecipeData';
const App: React.FC = () => {
const { data, loading } = useQuery<RecipeData | null>(GET_ALL_RECIPES, {
suspend: false
})
if (loading || !data) return <div>Loading</div>
return (
<div className="App">
<ul>
if(RecipeData.recipe !== null){
{
data.recipe.map(recipe =>
<li>{recipe.name}</li>
)
}
}
</ul>
</div>
);
}
export default App;
package.json :
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#types/jest": "24.0.23",
"#types/node": "12.12.14",
"#types/react": "16.9.13",
"#types/react-dom": "16.9.4",
"apollo": "^2.21.1",
"apollo-boost": "^0.4.4",
"apollo-cache-inmemory": "^1.6.3",
"apollo-client": "^2.6.4",
"apollo-link-http": "^1.5.16",
"react": "^16.12.0",
"react-apollo": "^3.1.3",
"react-apollo-hooks": "^0.5.0",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0",
"typescript": "^3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"apollo:generate": "apollo codegen:generate --target typescript --excludes=node_modules/* --includes=**/*.tsx --endpoint http://localhost:4000 --tagName=gql --outputFlat src/generated"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

You can't put statements in JSX curly braces, only expressions. You can replace the if statement with an inline && expression.
return (
<div className="App">
<ul>
{
RecipeData.recipe !== null && data.recipe.map(recipe =>
<li>{recipe.name}</li>
)
}
</ul>
</div>

Related

how to use nodejs modules in nw/react app

I'm trying to build an app with NW ans react and i'm having trouble calling node js modules fron react like the "fs" module i got the following error: TypeError: fs.readdir is not a function
this is my folders structure:
public
-----files generated by CRA
src
-----main.js
-----handlers.js
-----files generated by CRA
package.json
{
"name": "nw-react",
"version": "0.1.0",
"private": true,
"main": "./public/main.js",
"dependencies": {
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"nw" : "nw ."
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"cross-env": "^7.0.3",
"nw": "^0.64.1"
}
}
main.js
nw.Window.open("http://localhost:3000", {}, function (win) {});
handlers.js
const fs = require("fs");
exports.getFiles = cb => {
const dir = "C:\\Users\\pc\\Documents";
fs.readdir(dir, (err, files) => {
cb(files);
});
};
App.js
import { useState, useEffect } from "react";
import logo from "./logo.svg";
import "./App.css";
const handlers = require("./handler");
function App() {
const [files, setFiles] = useState([]);
useEffect(() => {
handlers.getFiles(files => {
setFiles(files);
});
}, []);
return (
<div className='App'>
{files.map(file => (
<h1 key={file}>{file}</h1>
))}
</div>
);
}
export default App;
Add this to your package.json:
"node-remote": [
"http://localhost:3000",
"file://*"
]
You may also need this:
"eslintConfig": {
"extends": "react-app",
"globals": {
"nw": true
}
}
You cannot use fs module in browser.. it is node core module.

why antd switch rendering is not working?

I try to make typescript react setting
and then just install antd
$ yarn add antd
and fix app.tsx like this
import React from "react";
import logo from "./logo.svg";
import "./App.css";
import { Switch } from "antd";
function App() {
function onChange(checked: any) {
console.log(`switch to ${checked}`);
}
return (
<div className="App">
<Switch defaultChecked onChange={onChange} />
</div>
);
}
export default App;
but i can't see right switch
https://i.stack.imgur.com/rmGjJ.png
i want to look like this
https://i.stack.imgur.com/xKVWW.png
and this is my package.json
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^12.0.0",
"#testing-library/user-event": "^13.2.1",
"#types/jest": "^27.0.1",
"#types/node": "^16.7.13",
"#types/react": "^17.0.20",
"#types/react-dom": "^17.0.9",
"antd": "^4.18.8",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"typescript": "^4.4.2",
"web-vitals": "^2.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
How to solve this rendering problem?

'useState' is defined but never used EVENTHOUGH I have used useState hook

I have simple functional component like below.
import React, { useState } from 'react';
import { connect } from 'react-redux';
import JoinRoomInputs from '../../../components/JoinRoomPage/JoinRoomInputs/JoinRoomInputs';
import { State } from '../../../store/states/states';
interface Props {
isRoomHost: boolean;
}
const JoinRoomContent = ({ isRoomHost }:Props) => {
const [roomIdValue, setRoomIdValue] = useState('');
const [nameValue, setNameValue] = useState('');
return (
<>
<JoinRoomInputs roomIdValue={roomIdValue} setRoomIdValue={setRoomIdValue} nameValue={nameValue} setNameValue={setNameValue} isRoomHost={isRoomHost} />
</>
);
};
const mapStateToProps = (state:State) => {
return {
...state
}
}
export default connect(mapStateToProps)(JoinRoomContent);
as you can see I have used useState hook in two lines.
But I'm getting yellow warning saying this.
src\components\JoinRoomPage\JoinRoomContent\JoinRoomContent.tsx
Line 1:17: 'useState' is defined but never used #typescript-eslint/no-unused-vars
Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.
And what is more curious is line 17 is this );
This doesn't make any sense to me.
What am I doing wrong here ?
below is my package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#reduxjs/toolkit": "^1.6.2",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"#types/jest": "^24.9.1",
"#types/node": "^12.20.36",
"#types/react": "^16.14.20",
"#types/react-dom": "^16.9.14",
"#types/react-redux": "^7.1.20",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.6",
"react-router-dom": "^6.0.2",
"react-scripts": "4.0.3",
"typescript": "^4.1.6"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"typescript-plugin-css-modules": "^3.4.0"
}
}
Restart your server or kill the port with the following command:
sudo kill $(sudo lsof -t -i:PORT)

TypeError: Cannot read property 'props' of undefined in ReactJS

I'm trying to create a simple app with ReactJS, but I get this error and I don't know why.
This is my App.js.
import React from 'react';
import { connect } from 'react-redux';
import Category from './components/Category';
//import News from './components/Noticias';
import './App.css';
function App() {
const { categories } = this.props;
return (
<div className="App">
<Category categories={categories} />
{/*<News />*/}
</div>
);
}
const mapStateToProps = state => {
const { Categorias: {data: categories}} = state;
return {
categories,
}
}
const mapDispatchToProps = dispatch => ({})
export default connect(mapStateToProps, mapDispatchToProps)(App);
I'm working under v14.4.0 of node.js and this is my package.json for check react version, redux version, etc.
{
"name": "noticias",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-redux": "^7.2.1",
"react-scripts": "3.4.3",
"redux": "^4.0.5",
"redux-form": "^8.3.6"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I'm checking but I don't find the error, can you help me?
You might write this,
function App({ categories }) {
// const { categories } = this.props;
return (
<div className="App">
<Category categories={categories} />
{/*<News />*/}
</div>
);
}

Does ReactTags work with React 16.12 applications?

I wanted to use ReactTags with my React 16.12 application following the instructions here -- https://www.npmjs.com/package/react-tag-input . I installed react-dnd 11.1.1 . Below is my package.json file
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.0",
"#testing-library/user-event": "^7.2.1",
"bootstrap": "^4.4.1",
"jquery": "^1.9.1",
"react": "^16.12.0",
"react-bootstrap": "^1.0.0-beta.17",
"react-device-detect": "^1.12.1",
"react-dnd": "^11.1.1",
"react-dnd-html5-backend": "^11.1.1",
"react-dom": "^16.12.0",
"react-hamburger-menu": "^1.2.1",
"react-native-flash-message": "^0.1.15",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.1",
"react-tag-input": "^6.4.3",
"typescript": "^3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "NODE_ENV=development react-scripts build",
"build:prod": "NODE_ENV=production react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"proxy": "http://localhost:8000",
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I'm trying to implement the tags like so
import React from 'react';
import ReactDOM from 'react-dom';
import { WithContext as ReactTags } from 'react-tag-input';
const KeyCodes = {
comma: 188,
enter: 13,
};
const delimiters = [KeyCodes.comma, KeyCodes.enter];
class CoopTypes extends React.Component {
constructor(props) {
super(props);
const tags = props.values.map(result => (
{
id: result.id,
text: result.name
}));
this.state = {
tags: tags,
suggestions: props.values.map(result => ({
id: result.id,
text: result.name
}))
};
this.handleDelete = props.handleDelete;
this.handleAddition = props.handleAddition;
this.handleDrag = this.handleDrag.bind(this);
}
handleDelete(i) {
const { tags } = this.state;
this.setState({
tags: tags.filter((tag, index) => index !== i),
});
}
handleAddition(tag) {
this.setState(state => ({ tags: [...state.tags, tag] }));
}
handleDrag(tag, currPos, newPos) {
const tags = [...this.state.tags];
const newTags = tags.slice();
newTags.splice(currPos, 1);
newTags.splice(newPos, 0, tag);
// re-render
this.setState({ tags: newTags });
}
render() {
const { tags, suggestions } = this.state;
return (
<div>
<ReactTags tags={tags}
suggestions={suggestions}
handleDelete={this.handleDelete}
handleAddition={this.handleAddition}
handleDrag={this.handleDrag}
delimiters={delimiters} />
</div>
)
}
};
export default CoopTypes;
but I'm running into the below error when I start up my app
TypeError: (0 , _reactDnd.DragDropContext) is not a function
./node_modules/react-tag-input/dist-modules/components/ReactTags.js
node_modules/react-tag-input/dist-modules/components/ReactTags.js:538
535 | };
536 |
537 | module.exports = {
> 538 | WithContext: (0, _reactDnd.DragDropContext)(_reactDndHtml5Backend2.default)(ReactTags),
539 | WithOutContext: ReactTags,
540 | KEYS: _constants.KEYS
541 | };
View compiled
Any thoughts on how to address this? Maybe the component doesn't work anymore with newer React versions?
react-tag-input using DragDropContext from react-dnd which is deprecated in v8 of react-dnd and hence you need to use a lower version on react-dnd in your App to make it work
You can install v7.6.2 of react-dnd and react-dnd-html5-backend
"react-dnd": "^7.6.2",
"react-dnd-html5-backend": "^7.6.2",
Working Demo

Resources