Date not passed via props in react js [Now fixed]

  • JavaScript
  • Thread starter shivajikobardan
  • Start date
In summary, The App component in the App.js file imports the Subscription component and creates an array of subscription objects. Then, the App component returns a div with the Subscription component passed in with different values for each subscription object. The Subscription component in the Subscription.js file returns the date, title, and amount of each subscription in a formatted way.
  • #1
shivajikobardan
674
54
App.js:
import './App.css';
import Subscription from './components/Subscription';
// import Myroutes from "./Myroutes.js";

function App() {
  let subscriptions = [{ id: "1", date: new Date("2021", "3", "23"), title: "Monthly Subscription", amount: "125.60" },
  { id: "2", date: new Date("2020", "06", "28"), title: "Annual Subscription", amount: "1125.60" },
  { id: "1", date: new Date("2021", "09", "05"), title: "Quarterly Subscription", amount: "425.60" }]

  return (
    <>
      <div className='App'>
        <Subscription datePassed={subscriptions[0].date.toISOString} titlePassed={subscriptions[0].title} amountPassed={subscriptions[0].amount} />
        <Subscription datePassed={subscriptions[1].date.toISOString} titlePassed={subscriptions[1].title} amountPassed={subscriptions[1].amount} />
        <Subscription datePassed={subscriptions[2].date.toISOString} titlePassed={subscriptions[2].title} amountPassed={subscriptions[2].amount} />
      </div>
    </>
  )
};

export default App;

Subscription.js component:
import React from 'react'
import "./Subscription.css"
function Subscription(props) {
  return (
    <>
      <div>{props.datePassed}</div>
      <h2 className="subscription-title">{props.titlePassed}</h2>
      <div>{props.amountPassed}</div>
    </>)
}

export default Subscription

Currrent Output that I'm getting:
1672147338777.png

Expected Output:

I need date to come at the top.
 
Technology news on Phys.org
  • #2
FIXED:
date.toISOString should be date.toISOString()
 
  • Like
Likes jedishrfu and .Scott

FAQ: Date not passed via props in react js [Now fixed]

What is the issue with not passing a date through props in React JS?

The issue with not passing a date through props in React JS is that the component will not have access to the date, making it difficult to manipulate or display the date in the component.

How can I pass a date through props in React JS?

You can pass a date through props in React JS by including the date as a prop in the component's parent element, and then accessing it in the child component using the "this.props" syntax.

Can I pass a date through props to multiple components in React JS?

Yes, you can pass a date through props to multiple components in React JS by including the date as a prop in each of the parent components and then accessing it in the child components using the "this.props" syntax.

What are the benefits of passing a date through props in React JS?

The benefits of passing a date through props in React JS include easier manipulation and display of the date in the component, better organization and management of data, and improved reusability of components.

Is there an alternative to passing a date through props in React JS?

Yes, an alternative to passing a date through props in React JS is to use a global state management tool like Redux or Context API to store and access the date in different components.

Similar threads

Replies
2
Views
1K
Replies
8
Views
1K
Replies
2
Views
1K
Replies
5
Views
604
Replies
10
Views
1K
Replies
32
Views
2K
Replies
5
Views
498
Replies
2
Views
1K
Back
Top