Parsing error: Unexpected token React in functional component - reactjs

I am learning React and getting an error while displaying values from props in a functional component. I know the syntax is wrong but don't know how to fix it.
When the same functional component was written as a class component it was working fine.But in functional component its giving syntax error.
Code:
App.js:
import React, { Component } from 'react';
import './App.css';
import Crypto from './Component/Crypto';
class App extends Component {
constructor(){
super();
this.state={
data: [
{
name:'Bitcoin',
id:1,
value:'2000'
},
{
name:'Ripple',
id:2,
value:'60'
},
{
name:'Etherium',
id:3,
value:'250'
}
]
}
}
render() {
return (
<div className="App">
<Crypto data={this.state.data}/>
</div>
);
}
}
export default App;
Crypto.js
import React from 'react';
const Crypto=(props)=> {
return (
<div className="App">
{const showData=props.data.map((info)=>
return (
<div key={info.id}>
<h3>{info.name}</h3>
<h2>${info.value}</h2>
</div>
)
)}
{showData}
</div>
);
}
export default Crypto;
error: {const showData=props.data.map((info)=>

return (
<div className="App">
{const showData=props.data.map((info)=> //<==== remove const showData
return (
<div key={info.id}>
<h3>{info.name}</h3>
<h2>${info.value}</h2>
</div>
)
)}
{showData} //<==== remove {showData}
</div>
);
return (
<div className="App">
{props.data.map((info)=>
return (
<div key={info.id}>
<h3>{info.name}</h3>
<h2>${info.value}</h2>
</div>
)
)}
</div>
);
The problem I noticed is you declare the variable in your return statement. You should instead remove it showData and using solely map function to return the desired output

You can try
const Crypto = (props) => {
return (
<div className="row">
{props.data.map((info) => (
<ul className="list-group"> // if you are using map better to use index
<h3>Name {info.name}</h3>
<li className="list-group-item">Value: {info.value}</li>
</ul>
))}
</div>
)
};
export default Crypto;

Related

How to set state properly in react js

I have 2 react components -
import React, {Component} from 'react'
import './App.css';
import ContactCard from './components/ContactCard'
class App extends Component {
render() {
return (
<div className="App-header">
<ContactCard name="hi 1" age={36} email="hu#yahoo.com"/>
<ContactCard name="hi 2" age={67} email="hi#yahoo.com"/>
<ContactCard name="hi 2" age={42} email="he#yahoo.com"/>
</div>
);
}
}
export default App;
import React, { Component } from "react";
class ContactCard extends Component {
state = {
showAge: false,
};
setAge = () => {
this.setState({
showAge: !this.state.showAge,
});
};
render() {
return (
<div className="contactCard">
<div className="userDetails">
<h2>Name: {this.props.name}</h2>
<p>Email: {this.props.email}</p>
<button onClick={this.state.setAge}>Show Age</button>
{this.state.showAge && <p>Age: {this.props.age}</p>}
</div>
</div>
);
}
}
export default ContactCard;
Toggle button is not working. i have set the state before render method. its not mandatory to set the state in the constructor.
Now the error is gone but still toggle button not working.
Whats going wrong?
You are setting state the wrong way. It should be:
setAge = () => {
this.setState({
showAge: !this.state.showAge
});
};
Edit
In order to make your app work as expected, you also have to set the click handler properly:
<button onClick={this.setAge}>Show Age</button>
You have to update your code its this.setAge not this.state.setAge
render() {
return (
<div className="contactCard">
<div className="userDetails">
<h2>Name: {this.props.name}</h2>
<p>Email: {this.props.email}</p>
<button onClick={this.setAge}>Show Age</button>
{this.state.showAge && <p>Age: {this.props.age}</p>}
</div>
</div>
);
}

I have assigned a unique key prop bu keep getting the error

I keep getting the error below despite trying to set a unique key
index.js:1 Warning: Each child in a list should have a unique "key" prop....
DETAILS
I have a component that is rendering and am passing an key={id} to the child component the id is unique and will render through another prop but I keep getting an error. I think I'm doing something stupid but cannot figure it out
PARENT COMPONENT
import React, { Fragment } from 'react';
import ReactDOM from 'react-dom';
import Card from './card';
const CardsBlock = props => {
const cardRender = props.Cards.map(Cards => {
const { title, url, id } = Cards;
return (
<Fragment>
<Card key={id} id={id} title={title} url={url} />
</Fragment>
);
});
return (
<div
id='card-block'
className='blue container-thin cardblock '>
<h2>FACTS</h2>
{cardRender}
</div>
);
};
export default CardsBlock;
CHILD COMPONENT
import React from 'react';
import ReactDOM from 'react-dom';
import 'materialize-css';
const Card = props => {
//deconstruct props here
const { title, url, id } = props
return (
<div id={"card-"+id} className='blue lighten-3 card container-thin lighten-4'>
<div className='card-left'>
<i class='material-icons grey-text'>more_vert</i>
</div>
<div class='card-middle'>
<a href='http://www.jimmyyukka.com'>
<p>{title} </p>
</a>
</div>
<div className='card-right'>
<span>
<i class='material-icons grey-text'>mode_edit</i>
</span>
<span>
<i class='material-icons grey-text'>clear</i>
</span>
</div>
</div>
);
};
export default Card;
1.Don't need to define fragment if you are passing value to child component.
2.You can use destructuring as used or pass object and destructure inside child component.
Define like this using destructuring,
const CardsBlock = props => {
const cardRender = props.Cards.map(({title,url,id})=>
<Card key={id} id={id} title={title} url={url} />
);
return (
<div
id='card-block'
className='blue container-thin cardblock '>
<h2>FACTS</h2>
{cardRender}
</div>
);
};
import React, { Fragment } from 'react';
import ReactDOM from 'react-dom';
import Card from './card';
const CardsBlock = props => {
const cardRender = props.Cards.map(record=> {
const { title, url, id } = Cards;
return (
<Fragment>
<Card key={record.id} id={record.id} title={record.title} url={record.url} />
</Fragment>
);
});
return (
<div
id='card-block'
className='blue container-thin cardblock '>
<h2>FACTS</h2>
{cardRender}
</div>
);
};
export default CardsBlock;

react - children component not showing

I'm trying to insert a child component inside another child component, but my code is not working. Following codes are the structure i'm trying to build.
const AddProductPage= () => {
return (
<PageTemplate>
<ProductTemplate>
<AddProduct />
</ProductTemplate>
</PageTemplate>
);
};
const PageTemplate= ({children}) => {
return (
<div className={cx('pagetemplate')}>
<HeaderContainer />
<main>
{children}
</main>
<Footer />
</div>
);
};
class ProductTemplate extends Component {
render() {
return (
<div className={cx('product-template')}>
...
<div className={cx('display')}>
{this.props.children}
</div>
</div>
);
}
}
class AddProduct extends Component {
render() {
return (
<div className={cx('addproduct')}>
addproduct
</div>
);
}
}
I'm trying to insert AddProduct component in ProductTemplate component as a child, which is inserted in PageTemplate component as a child. AddProductPage, however, is not showing AddProduct component. I'd be grateful if anyone can help.
I've run your code. May be you'r missing default export statements.
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import AddProductPage from './AddProductPage';
ReactDOM.render(<AddProductPage />, document.getElementById('root'));
AddProductPage.js
import React from 'react';
import PageTemplate from './PageTemplate';
import ProductTemplate from './ProductTemplate';
import AddProduct from './AddProduct';
const AddProductPage= () => {
return (
<PageTemplate>
<ProductTemplate>
<AddProduct />
</ProductTemplate>
</PageTemplate>
);
};
export default AddProduct;
PageTemplate.js
import React from 'react';
const PageTemplate= ({children}) => {
return (
<div>
<main>
{children}
</main>
</div>
);
};
export default PageTemplate;
ProductTemplate.js
import React,{Component} from 'react';
class ProductTemplate extends Component {
render() {
return (
<div>
<div>
{this.props.children}
</div>
</div>
);
};
};
export default ProductTemplate;
AddProduct.js
import React, {Component} from 'react';
class AddProduct extends Component {
render() {
return (
<div>
addproduct
</div>
);
};
};
export default AddProduct;
Output is this :
addproduct
Here`s a Fiddle of your code
Works Fine!. I have removed cx API. I suppose the problem might be with className resolution. Check in the dom hierarchy weather the Children DOM Node exists and they have received their respective classnames.
const PageTemplate= ({children}) => {
return (
<div className={'pagetemplate'}>
<HeaderContainer />
<main>
{children}
</main>
<Footer />
</div>
);
};

How to structure component export without export default?

I am trying not to use export default in my components but rather export function..
My components are structured something like this:
function Hero(props) {
return (
<section className="hero">
<div className="wrapper">
<div className="content">
<h1>{props.title}</h1>
<p>{props.text}</p>
</div>
</div>
</section>
);
}
export default Hero;
when I do import {Hero} from './components/hero/hero.js' it works ok for this component, however if I have a component that is receiving props. Like this, it does not work:
export function Chart() {
return (
<section className="chart">
<div className="wrapper">
<h2>{this.props.children}</h2>
<div className="img-container">
<img className="desktop" src={chart} />
<img className="mobile" src={mobileChart} />
</div>
</div>
</section>
);
}
I am trying to avoid export default and avoid using something like
class Chart extends React.Component {
render() {
return (
<section className="chart">
<div className="wrapper">
<h2>{this.props.children}</h2>
<div className="img-container">
<img className="desktop" src={chart} alt="Mortgage Rate Comparison Chart"/>
<img className="mobile" src={mobileChart} alt="Mortgage Rate Comparison Chart"/>
</div>
</div>
</section>
);
}
}
export default Chart;
When I try to switch this I am getting an error, i'm not sure the proper syntax for exporting this way. It was a request made by my lead dev and he is out of office this week.
When you have one model per module, default exports are preferred as per ECMAScript
If your code is like this in the file:
ChartComponent.js
export function Chart(props) {
return (
<section className="chart">
<div className="wrapper">
<h2>{props.children}</h2>
<div className="img-container">
<img className="desktop" src={chart} />
<img className="mobile" src={mobileChart} />
</div>
</div>
</section>
);
}
You have to import it this way:
With export default
import Chart from "./ChartComponent";
Without export default
import { Chart } from "./ChartComponent";
<Chart>
<div>
child elements
</div>
</Chart>
This should pass the children into your component if you are listening for props.
If you have multiple components in single file, you can export them in these ways:
ExampleMultipleComponents.js
export const ComponentI = class ComponentI extends React.Component {....};
export const ComponentII = () => (<div> ... </div>);
or like:
const ComponentI = class ComponentI extends React.Component {....};
const ComponentII = () => (<div> ... </div>);
export {
ComponentII,
ComponentI,
}
And them import them like:
import { ComponentI, ComponentII } from './ExampleMultipleComponents';
Hope this is helpful!

ReactJS: Image not rendering

I am trying to render static images from images folder under src directory, however, it is not rendering, if i try to import any of the images directly and then pass in jsx like commented portion in images declaration it renders fine, also tried with backgroundImage property of css, it does not render anything.
import React, { Component } from 'react';
import Pizza from './components/pizza';
import './App.css';
class App extends Component {
state = {
open: false,
active: null,
categories: []
}
componentDidMount() {
this.setState({
categories
})
}
render() {
return (
<div className="App">
<Pizza data={this.state}/>
</div>
);
}
}
const categories = [
{"title":"Italian Pizza","link":"../images/italian-pizza.jpg",
"id":"586537da62981d5fb8c21617",
"details": "aksnflafiafoasofhafhoahfohaofhoahfhashfohasfhofhahsofohahosf"}
];
export default App;
pizza.js
import React from 'react';
import PropTypes from 'prop-types';
import italian from '../images/italian-pizza.jpg'
const Pizza = ({data}) => {
const images = data.categories.map((d,i) => {
return (
<div key={i}>
{/* <img src={italian} /> */}
<img src={`${d.link}`} />
</div>
)
});
return (
<div className="pizza">
<header className="header">Pizza</header>
<div className="main">
{data.categories.map((category, index) => {
return (
<div key={index}>
<ul className="items">
{images}
{/* <li style={{backgroundImage: `url(require(italian))`}} /> */}
</ul>
</div>
)
})}
</div>
</div>
)
}
Pizza.propTypes = {
categories: PropTypes.array,
open: PropTypes.bool,
active: PropTypes.bool
}
export default Pizza;

Resources