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!
Related
When I use tag as shown below, I'm getting this error!
React TypeError: Class constructor Fullpage cannot be invoked without 'new'
It looks like something went wrong in line 1438:
renderWithHooks
node_modules/react-dom/cjs/react-dom.development.js:14803
this is my Fullpage.js file:
import React, {Component} from 'react';
import './Fullpage.css'
class Fullpage extends Comment{
render(){
const {children}=this.props
return(
<div className={`fullpage ${this.props.className || ''}`}>
{children}
</div>
);
}
}
export default Fullpage
App.js file:
import React, { Component } from 'react'
import data from './data.json';
import logo from './logo.svg';
import './App.css';
import { SocialIcon } from 'react-social-icons';
import Fullpage from './components/Fullpage.js'
class App extends Component{
render(){
return(
<div className="App container">
<div className="navigation">
</div>
<Fullpage className="first">
<h1 className="title" id={"title"}>
{data.title}
</h1>
<div>
<h2>
{data.subtitle}
</h2>
</div>
<div className="icons-wrapper">
{
Object.keys(data.links).map(key=>{
return(
<div className="icon">
<SocialIcon url={data.links[key]}/>
</div>
);
})
}
</div>
</Fullpage>
<div className="fullpage">
<h3>
{data.sections[0].title}
</h3>
<p>
{data.sections[0].items[0].content}
</p>
</div>
<div className="fullpage">
</div>
</div>
);
}
}
export default App;
Looks like there should be: class Fullpage extends Component not Comment.
I've got this functional component
import React from 'react'
function Header(){
return(
<div className='header'>
<h2>Logo</h2>
<p>Log In</p>
</div>
)
}
export default Header
When I will clicked on "Log In" I want to change my background-color in this class in class 'MainContainer'. Im a beginner in React. How Can I do it?
import React from 'react'
import RegistrationForm from '../LoginPage/RegistrationForm'
import './style2.scss'
class Main extends React.Component{
constructor(param){
super(param)
}
render(){
return(
<div className="mainContainer">
<RegistrationForm/>
</div>
)
}
}
export default Main
EDIT: Here is the class where I called this classes:
import React from 'react'
import Header from './components/LoginPage/Header'
import Main from './components/LoginPage/Main'
function App() {
return (
<div>
<Header/>
<Main/>
</div>
)
}
export default App;
Props can be passed from parent to child, not between siblings. So make a state in the App component and pass it down to Main and Header.
App
function App() {
const [loggingIn, setLoggingIn] = React.useState(false);
return (
<div>
<Header setLoggingIn={setLoggingIn} />
<Main loggingIn={loggingIn} />
</div>
)
}
Main
class Main extends React.Component{
constructor(param){
super(param)
}
render(){
return(
<div className="mainContainer" style={{ background: this.props.loggingIn ? 'red' : 'inherit' }}>
<RegistrationForm/>
</div>
)
}
}
Header
function Header({ setLoggingIn }){
return(
<div className='header'>
<h2>Logo</h2>
<p onClick={() => setLoggingIn(true)}>Log In</p>
</div>
)
}
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;
I keep getting a message that the item I'm trying to access via props is undefined. Can you please tell me why it's not working?
Here is where the instance where the props are attempting to be passed... I'm specifically talking about the tellus and yourTrial props.
import React from 'react'
import Info from './components/Info'
import Upsell from '../../../general/components/order/components/Upsell'
import FormTwo from '../../../general/components/forms/FormTwo'
import Footer from '../../../cbd-desktop/components/layout/Footer'
export default class Order extends React.Component {
render() {
return (
<div>
<div>
<div style={styles.header}>
<img style={styles.leaf} src="./resources/desktop-img/leaves_top.png" />
<img style={styles.logo} src="./resources/desktop-img/logo_white.png" />
</div>
<div style={styles.wrapper}>
<div style={styles.leftWrapper}>
<Info />
<Upsell styles={upsellStyles} />
</div>
<FormTwo styles={formStyles} tellus="Tell us where to send" yourTrial="YOUR TRIAL BOTTLE"} />
</div>
</div>
<Footer style={styles.footer}/>
</div>
)
}
}
And here is where I am trying to display these values on the child... towards the top in two h2s
import React from 'react'
import { connect } from 'react-redux'
import { stepTwoSubmit, saveBillingData } from
'../../actions/formStepTwoActions'
import { addReceiptProduct } from '../../actions/receiptActions'
import FormTwoInputs from './components/FormTwoInputsComponent.jsx'
import Throbber from '../throbber/Throbber'
import FormWarning from './components/FormWarningComponent.jsx'
import Button from '../../../cbd-desktop/components/layout/Button'
const mapStateToProps = state => ({
state:state,
config:state.config,
downsellProduct:state.downsell.downsellProduct || {},
receiptProducts:state.receipt.receiptProducts || []
})
const mapDispatchToProps = {
stepTwoSubmit,
saveBillingData,
addReceiptProduct
}
#connect(mapStateToProps, mapDispatchToProps)
export default class FormTwo extends React.Component {
constructor(props) {
super(props)
componentWillReceiveProps(nextProps) {
let formTwoResponse = nextProps.state.stepTwo.formTwoResponse
this.checkOrderStatus(formTwoResponse)
}
componentDidMount() {
this.fillMainOrder()
this.calculateViewers()
this.calculateTimer()
}
render() {
let { state, props, inputs, onInputFocus, saveInputVal, styles } = this,
CustomTag = props.labels ? 'label' : 'span',
{ submitting, formWarning } = state,
{ invalidInputID, text, visible } = formWarning
return (
<div style={styles.formWrapper}>
<p style={styles.yellowBanner}>{this.state.viewers} people viewing this product right now</p>
<div style={styles.formInnerWrapper}>
<div style={styles.headerWrapper}>
<h2 style={styles.header}>{this.props.tellus}</h2>
<h2 style={styles.header}>{this.props.yourTrial}</h2>
</div>
<div style={styles.weAccept}>
<p style={styles.weAcceptText}>We Accept:</p>
<img style ={styles.cardImage} src="resources/desktop-img/cards.png" />
</div>
<form onSubmit={this.submit}>
<FormTwoInputs onInputFocus={onInputFocus} saveInputVal={saveInputVal} CustomTag={CustomTag} styles={styles} />
<FormWarning visible={visible} invalidInputID={invalidInputID} text={text} />
<Button style={styles.button} buttonText="RUSH MY TRIAL" />
</form>
</div>
<img src="resources/desktop-img/secure.png" />
<div style={styles.urgencyWrapper}>
<div style={styles.urgencyTextWrapper}>
<span style={styles.redText}>{this.state.viewers} people are viewing this offer right now -</span>
<span style={styles.blueText}>{this.state.counter}</span>
</div>
<p style={styles.blueText}>Claim Your Bottle Now</p>
</div>
<Throbber throbberText='Confirming your shipment...' showThrobber={submitting} />
</div>
)
}
}
I am trying to re-write the entry point App.js javascript file as a class. I'm doing this because I want to call an onClick function when a user clicks on the log out link.
Here is the current code:
import React, { PropTypes } from 'react';
import { IndexLink, Link } from 'react-router';
const App = (props) => {
return (
<div>
<IndexLink className="mypad" to="home">Home</IndexLink>
<Link className="mypad" to="">Log Out</Link>
<br/>
<br/>
{props.children}
</div>
);
};
App.propTypes = {
children: PropTypes.element
};
export default App;
Here was my attempt at re-writing this as a class. It doesn't work. I get the error: 'ReferenceError: props is not defined'
class App extends React.Component {
constructor(props) {
super(props);
}
render(){
return (
<div>
<IndexLink className="mypad" to="home">Home</IndexLink>
<Link className="mypad" to="">Log Out</Link>
<br/>
<br/>
{props.children}
</div>
);
}
}
App.propTypes = {
children: PropTypes.element
};
export default App;
You need to do this.props once its a class
<div>
<IndexLink className="mypad" to="home">Home</IndexLink>
<Link className="mypad" to="">Log Out</Link>
<br/>
<br/>
{this.props.children}
</div>
when you write the component as a function, props gets passed in as an argument. When you do it as a class, props are bound to the instance of the component object.
add this => this.props.children