Trying to pass a function back to App.js and getting stuck REACTReact js onClick can't pass value to methodHow get value datapicker in react toobox custom?Get version number from package.json in React Redux (create-react-app)React className changes but CSS style do notFunctions and re-renders in React ContextHow to add Material-ui App Bar to class component?Can you pass this as an argument to a renderless component in React?Why does createRef() returns current: null?React Router structuring - passing functionsTrying to make calculations while passing props and functions through different components REACT

Are hand made posters acceptable in Academia?

Why is "la Gestapo" feminine?

Should a narrator ever describe things based on a character's view instead of facts?

Why would five hundred and five same as one?

What 1968 Moog synthesizer was used in the Movie Apollo 11?

Strange behavior in TikZ draw command

Showing mass murder in a kid's book

Error in master's thesis, I do not know what to do

Capacitor electron flow

Does capillary rise violate hydrostatic paradox?

Do I have to take mana from my deck or hand when tapping this card?

Started in 1987 vs. Starting in 1987

"Marked down as someone wanting to sell shares." What does that mean?

Mortal danger in mid-grade literature

What is the meaning of "You've never met a graph you didn't like?"

Taking the numerator and the denominator

Why doesn't Gödel's incompleteness theorem apply to false statements?

Friend wants my recommendation but I don't want to give it to him

Why didn’t Eve recognize the little cockroach as a living organism?

Derivative of an interpolated function

Make a Bowl of Alphabet Soup

Why can't I get pgrep output right to variable on bash script?

Mac Mini (2018) 10Gb Port Compatibility

A seasonal riddle



Trying to pass a function back to App.js and getting stuck REACT


React js onClick can't pass value to methodHow get value datapicker in react toobox custom?Get version number from package.json in React Redux (create-react-app)React className changes but CSS style do notFunctions and re-renders in React ContextHow to add Material-ui App Bar to class component?Can you pass this as an argument to a renderless component in React?Why does createRef() returns current: null?React Router structuring - passing functionsTrying to make calculations while passing props and functions through different components REACT













0















I have an app that allows me to add a list of clients by input. I also have a another component that allows me to choose one of the existing clients from a dropdown menu and create a 'new lesson' with them. This way I can log my lessons done with my clients. This all works perfectly... this is where I get stuck.



Everytime I add a new lesson, I want to take the duration of the lesson (in hours) and multiply that by the corresponding client's price. I then want to be able to display that profit and everytime I add a new lesson with a new profit, I want it all to add up to a totalProfit of all the lessons.



This is App.js



import React, Component from 'react'
import Clients from './components/Clients'
import AddClient from './components/AddClient'
import NavBar from './components/NavBar'
import AddLesson from './components/AddLesson'
import CalculateIncome from './components/CalculateIncome'

class App extends Component
state =
clients : [
// id : null , name : '', price : null
],
lessons : [
// id: null, name: '', time: null, date :null
],


deleteClient = (id) =>
let clients = this.state.clients.filter(client =>
return client.id !== id
)
this.setState(
clients: clients
)


addClient = (newClient) =>
newClient.id = Math.random();
let clients = [...this.state.clients, newClient];
clients.sort((a, b) => a.name.localeCompare(b.name))
this.setState(
clients: clients,
)


addLesson = (newLesson) =>
newLesson.id = Math.random();
newLesson.date = Date();
let lessons = [...this.state.lessons, newLesson];
this.setState(
lessons: lessons,
)


render()
return (
<div className="App">
<NavBar/>
<Clients clients=this.state.clients deleteClient=this.deleteClient lessons=this.state.lessons/>
<AddClient addClient=this.addClient/>
<AddLesson addLesson=this.addLesson lessons=this.state.lessons clients=this.state.clients income=this.state.income />
<CalculateIncome lessons=this.state.lessons clients=this.state.clients/>
</div>
);



export default App;


This is CalculateIncome.js



import React, Component from 'react'

class CalculateIncome extends Component
state =
totalIncome:0


calculation = () =>
this.props.lessons.filter(lesson =>
this.props.clients.filter(client =>
if (client.name === lesson.name)
let price = client.price
let time = lesson.time
return(price, time)

return (
this.setState(
totalIncome : (price * time)
)
)
)
)


render()
return ( this.props.lessons.length ?
(this.state.totalIncome)
:
(<div className="text-center my-5">You are broke</div>)
)



export default CalculateIncome


If you need to see any other components including AddLesson.js and Clients.js please let me know I will post it up as well.










share|improve this question






















  • Excuse me but what exactly is your question ?

    – Coder949
    Mar 7 at 20:38











  • I have some components: App.js, Clients.js, AddClient.js AddLesson.js, and CalculateIncome.js. I am able to add clients and use those existing clients to add a lesson corresponding to each client. Each lesson has a time duration and each client has a price per hour. I want to create a function that will multiply (lesson.time * client.price) so I can know my income. My question is where would I put this function, would I put it straight in App.js or in AddLesson.js or would it just be its own calculation in CalculateIncome.js and how would I pass these properties up and down the components?

    – josephT
    Mar 7 at 20:43















0















I have an app that allows me to add a list of clients by input. I also have a another component that allows me to choose one of the existing clients from a dropdown menu and create a 'new lesson' with them. This way I can log my lessons done with my clients. This all works perfectly... this is where I get stuck.



Everytime I add a new lesson, I want to take the duration of the lesson (in hours) and multiply that by the corresponding client's price. I then want to be able to display that profit and everytime I add a new lesson with a new profit, I want it all to add up to a totalProfit of all the lessons.



This is App.js



import React, Component from 'react'
import Clients from './components/Clients'
import AddClient from './components/AddClient'
import NavBar from './components/NavBar'
import AddLesson from './components/AddLesson'
import CalculateIncome from './components/CalculateIncome'

class App extends Component
state =
clients : [
// id : null , name : '', price : null
],
lessons : [
// id: null, name: '', time: null, date :null
],


deleteClient = (id) =>
let clients = this.state.clients.filter(client =>
return client.id !== id
)
this.setState(
clients: clients
)


addClient = (newClient) =>
newClient.id = Math.random();
let clients = [...this.state.clients, newClient];
clients.sort((a, b) => a.name.localeCompare(b.name))
this.setState(
clients: clients,
)


addLesson = (newLesson) =>
newLesson.id = Math.random();
newLesson.date = Date();
let lessons = [...this.state.lessons, newLesson];
this.setState(
lessons: lessons,
)


render()
return (
<div className="App">
<NavBar/>
<Clients clients=this.state.clients deleteClient=this.deleteClient lessons=this.state.lessons/>
<AddClient addClient=this.addClient/>
<AddLesson addLesson=this.addLesson lessons=this.state.lessons clients=this.state.clients income=this.state.income />
<CalculateIncome lessons=this.state.lessons clients=this.state.clients/>
</div>
);



export default App;


This is CalculateIncome.js



import React, Component from 'react'

class CalculateIncome extends Component
state =
totalIncome:0


calculation = () =>
this.props.lessons.filter(lesson =>
this.props.clients.filter(client =>
if (client.name === lesson.name)
let price = client.price
let time = lesson.time
return(price, time)

return (
this.setState(
totalIncome : (price * time)
)
)
)
)


render()
return ( this.props.lessons.length ?
(this.state.totalIncome)
:
(<div className="text-center my-5">You are broke</div>)
)



export default CalculateIncome


If you need to see any other components including AddLesson.js and Clients.js please let me know I will post it up as well.










share|improve this question






















  • Excuse me but what exactly is your question ?

    – Coder949
    Mar 7 at 20:38











  • I have some components: App.js, Clients.js, AddClient.js AddLesson.js, and CalculateIncome.js. I am able to add clients and use those existing clients to add a lesson corresponding to each client. Each lesson has a time duration and each client has a price per hour. I want to create a function that will multiply (lesson.time * client.price) so I can know my income. My question is where would I put this function, would I put it straight in App.js or in AddLesson.js or would it just be its own calculation in CalculateIncome.js and how would I pass these properties up and down the components?

    – josephT
    Mar 7 at 20:43













0












0








0








I have an app that allows me to add a list of clients by input. I also have a another component that allows me to choose one of the existing clients from a dropdown menu and create a 'new lesson' with them. This way I can log my lessons done with my clients. This all works perfectly... this is where I get stuck.



Everytime I add a new lesson, I want to take the duration of the lesson (in hours) and multiply that by the corresponding client's price. I then want to be able to display that profit and everytime I add a new lesson with a new profit, I want it all to add up to a totalProfit of all the lessons.



This is App.js



import React, Component from 'react'
import Clients from './components/Clients'
import AddClient from './components/AddClient'
import NavBar from './components/NavBar'
import AddLesson from './components/AddLesson'
import CalculateIncome from './components/CalculateIncome'

class App extends Component
state =
clients : [
// id : null , name : '', price : null
],
lessons : [
// id: null, name: '', time: null, date :null
],


deleteClient = (id) =>
let clients = this.state.clients.filter(client =>
return client.id !== id
)
this.setState(
clients: clients
)


addClient = (newClient) =>
newClient.id = Math.random();
let clients = [...this.state.clients, newClient];
clients.sort((a, b) => a.name.localeCompare(b.name))
this.setState(
clients: clients,
)


addLesson = (newLesson) =>
newLesson.id = Math.random();
newLesson.date = Date();
let lessons = [...this.state.lessons, newLesson];
this.setState(
lessons: lessons,
)


render()
return (
<div className="App">
<NavBar/>
<Clients clients=this.state.clients deleteClient=this.deleteClient lessons=this.state.lessons/>
<AddClient addClient=this.addClient/>
<AddLesson addLesson=this.addLesson lessons=this.state.lessons clients=this.state.clients income=this.state.income />
<CalculateIncome lessons=this.state.lessons clients=this.state.clients/>
</div>
);



export default App;


This is CalculateIncome.js



import React, Component from 'react'

class CalculateIncome extends Component
state =
totalIncome:0


calculation = () =>
this.props.lessons.filter(lesson =>
this.props.clients.filter(client =>
if (client.name === lesson.name)
let price = client.price
let time = lesson.time
return(price, time)

return (
this.setState(
totalIncome : (price * time)
)
)
)
)


render()
return ( this.props.lessons.length ?
(this.state.totalIncome)
:
(<div className="text-center my-5">You are broke</div>)
)



export default CalculateIncome


If you need to see any other components including AddLesson.js and Clients.js please let me know I will post it up as well.










share|improve this question














I have an app that allows me to add a list of clients by input. I also have a another component that allows me to choose one of the existing clients from a dropdown menu and create a 'new lesson' with them. This way I can log my lessons done with my clients. This all works perfectly... this is where I get stuck.



Everytime I add a new lesson, I want to take the duration of the lesson (in hours) and multiply that by the corresponding client's price. I then want to be able to display that profit and everytime I add a new lesson with a new profit, I want it all to add up to a totalProfit of all the lessons.



This is App.js



import React, Component from 'react'
import Clients from './components/Clients'
import AddClient from './components/AddClient'
import NavBar from './components/NavBar'
import AddLesson from './components/AddLesson'
import CalculateIncome from './components/CalculateIncome'

class App extends Component
state =
clients : [
// id : null , name : '', price : null
],
lessons : [
// id: null, name: '', time: null, date :null
],


deleteClient = (id) =>
let clients = this.state.clients.filter(client =>
return client.id !== id
)
this.setState(
clients: clients
)


addClient = (newClient) =>
newClient.id = Math.random();
let clients = [...this.state.clients, newClient];
clients.sort((a, b) => a.name.localeCompare(b.name))
this.setState(
clients: clients,
)


addLesson = (newLesson) =>
newLesson.id = Math.random();
newLesson.date = Date();
let lessons = [...this.state.lessons, newLesson];
this.setState(
lessons: lessons,
)


render()
return (
<div className="App">
<NavBar/>
<Clients clients=this.state.clients deleteClient=this.deleteClient lessons=this.state.lessons/>
<AddClient addClient=this.addClient/>
<AddLesson addLesson=this.addLesson lessons=this.state.lessons clients=this.state.clients income=this.state.income />
<CalculateIncome lessons=this.state.lessons clients=this.state.clients/>
</div>
);



export default App;


This is CalculateIncome.js



import React, Component from 'react'

class CalculateIncome extends Component
state =
totalIncome:0


calculation = () =>
this.props.lessons.filter(lesson =>
this.props.clients.filter(client =>
if (client.name === lesson.name)
let price = client.price
let time = lesson.time
return(price, time)

return (
this.setState(
totalIncome : (price * time)
)
)
)
)


render()
return ( this.props.lessons.length ?
(this.state.totalIncome)
:
(<div className="text-center my-5">You are broke</div>)
)



export default CalculateIncome


If you need to see any other components including AddLesson.js and Clients.js please let me know I will post it up as well.







html reactjs components jsx






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 20:17









josephTjosephT

165




165












  • Excuse me but what exactly is your question ?

    – Coder949
    Mar 7 at 20:38











  • I have some components: App.js, Clients.js, AddClient.js AddLesson.js, and CalculateIncome.js. I am able to add clients and use those existing clients to add a lesson corresponding to each client. Each lesson has a time duration and each client has a price per hour. I want to create a function that will multiply (lesson.time * client.price) so I can know my income. My question is where would I put this function, would I put it straight in App.js or in AddLesson.js or would it just be its own calculation in CalculateIncome.js and how would I pass these properties up and down the components?

    – josephT
    Mar 7 at 20:43

















  • Excuse me but what exactly is your question ?

    – Coder949
    Mar 7 at 20:38











  • I have some components: App.js, Clients.js, AddClient.js AddLesson.js, and CalculateIncome.js. I am able to add clients and use those existing clients to add a lesson corresponding to each client. Each lesson has a time duration and each client has a price per hour. I want to create a function that will multiply (lesson.time * client.price) so I can know my income. My question is where would I put this function, would I put it straight in App.js or in AddLesson.js or would it just be its own calculation in CalculateIncome.js and how would I pass these properties up and down the components?

    – josephT
    Mar 7 at 20:43
















Excuse me but what exactly is your question ?

– Coder949
Mar 7 at 20:38





Excuse me but what exactly is your question ?

– Coder949
Mar 7 at 20:38













I have some components: App.js, Clients.js, AddClient.js AddLesson.js, and CalculateIncome.js. I am able to add clients and use those existing clients to add a lesson corresponding to each client. Each lesson has a time duration and each client has a price per hour. I want to create a function that will multiply (lesson.time * client.price) so I can know my income. My question is where would I put this function, would I put it straight in App.js or in AddLesson.js or would it just be its own calculation in CalculateIncome.js and how would I pass these properties up and down the components?

– josephT
Mar 7 at 20:43





I have some components: App.js, Clients.js, AddClient.js AddLesson.js, and CalculateIncome.js. I am able to add clients and use those existing clients to add a lesson corresponding to each client. Each lesson has a time duration and each client has a price per hour. I want to create a function that will multiply (lesson.time * client.price) so I can know my income. My question is where would I put this function, would I put it straight in App.js or in AddLesson.js or would it just be its own calculation in CalculateIncome.js and how would I pass these properties up and down the components?

– josephT
Mar 7 at 20:43












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052140%2ftrying-to-pass-a-function-back-to-app-js-and-getting-stuck-react%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052140%2ftrying-to-pass-a-function-back-to-app-js-and-getting-stuck-react%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices