Proper way to pass functions between distant componentsReactJS: Why is passing the component initial state a prop an anti-pattern?Correct way to share functions between components in ReactPassing Props between componentsPassing messages between components in React.jsHow to pass function as props in React?Passing props to React component treeReact - passing unique React components as propsfunction passed as React prop is not appearing in called childReact overwrite component functionHow to pass component props to react-navigation's static navigationOptions function

Ethernet, Wifi and a little human psychology

Output a Super Mario Image

Examples of proofs by making reduction to a finite set

What is the mathematical notation for rounding a given number to the nearest integer?

Why don't Wizards use wrist straps to protect against disarming charms?

What next step can I take in solving this sudoku?

How do I say "quirky" in German without sounding derogatory?

Are space camera sensors usually round, or square?

Olympic game scoring

Can I conceal an antihero's insanity - and should I?

Can I see Harvest moon in India?

How to control the output voltage of a solid state relay

Python web-scraper to download table of transistor counts from Wikipedia

geschafft or geschaffen? which one is past participle of schaffen?

Why the car dealer is insisting on loan instead of cash

What was the ultimate objective of The Party in 1984?

Why don't airports use arresting gears to recover energy from landing passenger planes?

Which is the current decimal separator?

How can I discourage sharing internal API keys within a company?

How to publish superseding results without creating enemies

POSIX compatible way to get user name associated with a user ID

Make 2019 with single digits

Why does the speed of sound decrease at high altitudes although the air density decreases?

How would you control supersoldiers in a late iron-age society?



Proper way to pass functions between distant components


ReactJS: Why is passing the component initial state a prop an anti-pattern?Correct way to share functions between components in ReactPassing Props between componentsPassing messages between components in React.jsHow to pass function as props in React?Passing props to React component treeReact - passing unique React components as propsfunction passed as React prop is not appearing in called childReact overwrite component functionHow to pass component props to react-navigation's static navigationOptions function






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















In my top level component i have:



<NavigationComponent />
<Router>
<Switch>
<Route component=SomeComponent />
...
</Switch>
</Router>


now, NavigationComponent have a delete icon which when clicked should call a function. I want that function to come from SomeComponent (or some other components from whatever active route is).



I’ve tried wrapping all routes into context, but it feels so entangled.



Other option was to pass setDeleteCallback to SomeComponent which would (in componentDidMount) set the callback and then pass that to NavigationComponent through render props. Also feels entangled.



Is there some best practice way to do this kind of plumbing?



ps. i dont need coded solution, just an idea.










share|improve this question


























  • You should take a look at react-redux, it does exactly what you want

    – mthrsj
    Mar 28 at 12:10











  • @mthrsj i do use redux but i dont want my redux state to contain functions.

    – dee zg
    Mar 28 at 12:11











  • A state shouldn't necessarily contain functions. You can dispatch an action, as with any other case.

    – Estus Flask
    Mar 28 at 12:15












  • Not, that's not the idea. The NavigationComponent emit an action, and the SomeComponent detect the change through componentDidUpdate and then call the desired function. What do you think?

    – mthrsj
    Mar 28 at 12:17











  • @mrhrsj how would navigation know which action data to include and which delete action to disptch?

    – dee zg
    Mar 28 at 15:31

















0















In my top level component i have:



<NavigationComponent />
<Router>
<Switch>
<Route component=SomeComponent />
...
</Switch>
</Router>


now, NavigationComponent have a delete icon which when clicked should call a function. I want that function to come from SomeComponent (or some other components from whatever active route is).



I’ve tried wrapping all routes into context, but it feels so entangled.



Other option was to pass setDeleteCallback to SomeComponent which would (in componentDidMount) set the callback and then pass that to NavigationComponent through render props. Also feels entangled.



Is there some best practice way to do this kind of plumbing?



ps. i dont need coded solution, just an idea.










share|improve this question


























  • You should take a look at react-redux, it does exactly what you want

    – mthrsj
    Mar 28 at 12:10











  • @mthrsj i do use redux but i dont want my redux state to contain functions.

    – dee zg
    Mar 28 at 12:11











  • A state shouldn't necessarily contain functions. You can dispatch an action, as with any other case.

    – Estus Flask
    Mar 28 at 12:15












  • Not, that's not the idea. The NavigationComponent emit an action, and the SomeComponent detect the change through componentDidUpdate and then call the desired function. What do you think?

    – mthrsj
    Mar 28 at 12:17











  • @mrhrsj how would navigation know which action data to include and which delete action to disptch?

    – dee zg
    Mar 28 at 15:31













0












0








0


1






In my top level component i have:



<NavigationComponent />
<Router>
<Switch>
<Route component=SomeComponent />
...
</Switch>
</Router>


now, NavigationComponent have a delete icon which when clicked should call a function. I want that function to come from SomeComponent (or some other components from whatever active route is).



I’ve tried wrapping all routes into context, but it feels so entangled.



Other option was to pass setDeleteCallback to SomeComponent which would (in componentDidMount) set the callback and then pass that to NavigationComponent through render props. Also feels entangled.



Is there some best practice way to do this kind of plumbing?



ps. i dont need coded solution, just an idea.










share|improve this question
















In my top level component i have:



<NavigationComponent />
<Router>
<Switch>
<Route component=SomeComponent />
...
</Switch>
</Router>


now, NavigationComponent have a delete icon which when clicked should call a function. I want that function to come from SomeComponent (or some other components from whatever active route is).



I’ve tried wrapping all routes into context, but it feels so entangled.



Other option was to pass setDeleteCallback to SomeComponent which would (in componentDidMount) set the callback and then pass that to NavigationComponent through render props. Also feels entangled.



Is there some best practice way to do this kind of plumbing?



ps. i dont need coded solution, just an idea.







reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 12:07







dee zg

















asked Mar 28 at 11:33









dee zgdee zg

5,7914 gold badges20 silver badges41 bronze badges




5,7914 gold badges20 silver badges41 bronze badges















  • You should take a look at react-redux, it does exactly what you want

    – mthrsj
    Mar 28 at 12:10











  • @mthrsj i do use redux but i dont want my redux state to contain functions.

    – dee zg
    Mar 28 at 12:11











  • A state shouldn't necessarily contain functions. You can dispatch an action, as with any other case.

    – Estus Flask
    Mar 28 at 12:15












  • Not, that's not the idea. The NavigationComponent emit an action, and the SomeComponent detect the change through componentDidUpdate and then call the desired function. What do you think?

    – mthrsj
    Mar 28 at 12:17











  • @mrhrsj how would navigation know which action data to include and which delete action to disptch?

    – dee zg
    Mar 28 at 15:31

















  • You should take a look at react-redux, it does exactly what you want

    – mthrsj
    Mar 28 at 12:10











  • @mthrsj i do use redux but i dont want my redux state to contain functions.

    – dee zg
    Mar 28 at 12:11











  • A state shouldn't necessarily contain functions. You can dispatch an action, as with any other case.

    – Estus Flask
    Mar 28 at 12:15












  • Not, that's not the idea. The NavigationComponent emit an action, and the SomeComponent detect the change through componentDidUpdate and then call the desired function. What do you think?

    – mthrsj
    Mar 28 at 12:17











  • @mrhrsj how would navigation know which action data to include and which delete action to disptch?

    – dee zg
    Mar 28 at 15:31
















You should take a look at react-redux, it does exactly what you want

– mthrsj
Mar 28 at 12:10





You should take a look at react-redux, it does exactly what you want

– mthrsj
Mar 28 at 12:10













@mthrsj i do use redux but i dont want my redux state to contain functions.

– dee zg
Mar 28 at 12:11





@mthrsj i do use redux but i dont want my redux state to contain functions.

– dee zg
Mar 28 at 12:11













A state shouldn't necessarily contain functions. You can dispatch an action, as with any other case.

– Estus Flask
Mar 28 at 12:15






A state shouldn't necessarily contain functions. You can dispatch an action, as with any other case.

– Estus Flask
Mar 28 at 12:15














Not, that's not the idea. The NavigationComponent emit an action, and the SomeComponent detect the change through componentDidUpdate and then call the desired function. What do you think?

– mthrsj
Mar 28 at 12:17





Not, that's not the idea. The NavigationComponent emit an action, and the SomeComponent detect the change through componentDidUpdate and then call the desired function. What do you think?

– mthrsj
Mar 28 at 12:17













@mrhrsj how would navigation know which action data to include and which delete action to disptch?

– dee zg
Mar 28 at 15:31





@mrhrsj how would navigation know which action data to include and which delete action to disptch?

– dee zg
Mar 28 at 15:31












1 Answer
1






active

oldest

votes


















2
















How about this idea, sorry just as I wanted to write code did not put it in comments



state=onDelete:()=>

onDeleteHandler=func=>this.setState(onDelete:func)

<NavigationComponent onDelete=this.state.onDelete/>
<Router>
<Switch>
<Route component=()=><SomeComponent onDeleteHandler=this.onDeleteHandler/> />
...
</Switch>
</Router>


now someComponent should send its desired delete function back here, maybe on componentDidMount and NavigationComponent will receive it on componentDidUpdate as anew prop






share|improve this answer



























  • thanks for your input! yes, thats one option i’ve tried (with render props, tought). i will upvote for now just to see if someone else comes up with something we overlooked. if not, i’ll accept it.

    – dee zg
    Mar 28 at 15:40











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/4.0/"u003ecc by-sa 4.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%2f55396579%2fproper-way-to-pass-functions-between-distant-components%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2
















How about this idea, sorry just as I wanted to write code did not put it in comments



state=onDelete:()=>

onDeleteHandler=func=>this.setState(onDelete:func)

<NavigationComponent onDelete=this.state.onDelete/>
<Router>
<Switch>
<Route component=()=><SomeComponent onDeleteHandler=this.onDeleteHandler/> />
...
</Switch>
</Router>


now someComponent should send its desired delete function back here, maybe on componentDidMount and NavigationComponent will receive it on componentDidUpdate as anew prop






share|improve this answer



























  • thanks for your input! yes, thats one option i’ve tried (with render props, tought). i will upvote for now just to see if someone else comes up with something we overlooked. if not, i’ll accept it.

    – dee zg
    Mar 28 at 15:40
















2
















How about this idea, sorry just as I wanted to write code did not put it in comments



state=onDelete:()=>

onDeleteHandler=func=>this.setState(onDelete:func)

<NavigationComponent onDelete=this.state.onDelete/>
<Router>
<Switch>
<Route component=()=><SomeComponent onDeleteHandler=this.onDeleteHandler/> />
...
</Switch>
</Router>


now someComponent should send its desired delete function back here, maybe on componentDidMount and NavigationComponent will receive it on componentDidUpdate as anew prop






share|improve this answer



























  • thanks for your input! yes, thats one option i’ve tried (with render props, tought). i will upvote for now just to see if someone else comes up with something we overlooked. if not, i’ll accept it.

    – dee zg
    Mar 28 at 15:40














2














2










2









How about this idea, sorry just as I wanted to write code did not put it in comments



state=onDelete:()=>

onDeleteHandler=func=>this.setState(onDelete:func)

<NavigationComponent onDelete=this.state.onDelete/>
<Router>
<Switch>
<Route component=()=><SomeComponent onDeleteHandler=this.onDeleteHandler/> />
...
</Switch>
</Router>


now someComponent should send its desired delete function back here, maybe on componentDidMount and NavigationComponent will receive it on componentDidUpdate as anew prop






share|improve this answer















How about this idea, sorry just as I wanted to write code did not put it in comments



state=onDelete:()=>

onDeleteHandler=func=>this.setState(onDelete:func)

<NavigationComponent onDelete=this.state.onDelete/>
<Router>
<Switch>
<Route component=()=><SomeComponent onDeleteHandler=this.onDeleteHandler/> />
...
</Switch>
</Router>


now someComponent should send its desired delete function back here, maybe on componentDidMount and NavigationComponent will receive it on componentDidUpdate as anew prop







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 12:45

























answered Mar 28 at 12:32









Amir-MousaviAmir-Mousavi

1,2211 gold badge18 silver badges46 bronze badges




1,2211 gold badge18 silver badges46 bronze badges















  • thanks for your input! yes, thats one option i’ve tried (with render props, tought). i will upvote for now just to see if someone else comes up with something we overlooked. if not, i’ll accept it.

    – dee zg
    Mar 28 at 15:40


















  • thanks for your input! yes, thats one option i’ve tried (with render props, tought). i will upvote for now just to see if someone else comes up with something we overlooked. if not, i’ll accept it.

    – dee zg
    Mar 28 at 15:40

















thanks for your input! yes, thats one option i’ve tried (with render props, tought). i will upvote for now just to see if someone else comes up with something we overlooked. if not, i’ll accept it.

– dee zg
Mar 28 at 15:40






thanks for your input! yes, thats one option i’ve tried (with render props, tought). i will upvote for now just to see if someone else comes up with something we overlooked. if not, i’ll accept it.

– dee zg
Mar 28 at 15:40









Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















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%2f55396579%2fproper-way-to-pass-functions-between-distant-components%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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?