Git Product home page Git Product logo

Comments (20)

tbolt avatar tbolt commented on March 28, 2024 8

@backslash112 Here is how I was able to have the "onRightButtonPress" trigger a function in the sub-component.

The only dependency I used is https://github.com/Olical/EventEmitter which can be added:
var EventEmitter = require('wolfy87-eventemitter');

In my main file (index.io.js) I created a new event emitter outside of the class:
var rightButtonHandler = new EventEmitter();

Then, inside the class I created a method to be called when the button is pressed:
handleSaveButton() { rightButtonHandler.emitEvent('saveButtonPressed'); }

And right below that I am passing the event emitter as a property to the sub-component:
passProps: { events: rightButtonHandler }

Now, switching to the sub-component class, add this or update your componentDidMount method:
componentDidMount() { this.props.events.addListener('saveButtonPressed', this.saveConcert.bind(this)); }

The above will be calling this saveConcert:
saveConcert() { /// This is now called when the navigatorIOS right button is pressed. }

Hope this helps!

from react-native.

ericvicenti avatar ericvicenti commented on March 28, 2024 3

Currently the best way to do that is to create an EventEmitter in the owner of the NavigatorIOS, then you can pass it down to children using route.passProps. The child can mix in Subscribable.Mixin and then in componentDidMount, you can this.addListenerOn(this.props.events, 'myRightBtnEvent', this._handleRightBtnPress);

It is clear that this API needs improvement. We are actively working the routing API in Relay, and hopefully react-router, but we want NavigatorIOS to be usable independently. Maybe we should add an event emitter inside the navigator object, so child components can subscribe to various navigator activity: this.addListenerOn(this.props.navigator.events, 'rightButtonPress', this._handleRightBtnPress);

Feedback welcome!

from react-native.

ReadingSteiner avatar ReadingSteiner commented on March 28, 2024 2

@ericvicenti
Why not use

this.props.event.addListener('myRightBtnEvent',this._handleRightBtnPress)` ?

directly in the componentDidMount?

What is Subscribable.Mixin's job here?

from react-native.

ericvicenti avatar ericvicenti commented on March 28, 2024 1

Subscribable.Mixin is something we use internally to make sure nobody forgets to unsubscribe to an emitter from a component on unmount. The mixin will automatically remove subscriptions added with this.addListenerOn. We are currently phasing it out, though, because we will be transitioning to observables in an upcoming version of React.

For more info on the existing EventEmitter, see the github project here: https://github.com/facebook/emitter

from react-native.

nick avatar nick commented on March 28, 2024

btw I'd love to contribute this example to the official repo once it's done :-)

from react-native.

vjeux avatar vjeux commented on March 28, 2024

You are right, we need to have a todo app example otherwise we're not a real js lib :p

from react-native.

nick avatar nick commented on March 28, 2024

My thoughts exactly. I'm trying to give it the exact same functionality as the iOS demo app you build in the first tutorial. Except the tutorial for this will be about 1/10th the length ;-)

from react-native.

zertosh avatar zertosh commented on March 28, 2024

@nick I've got a similar issue - I need to be notified that a "back" happened #26. I'm currently hacking around it by having the NavigatorIOS children call a prop callback on componentWillUnmount.

from react-native.

nick avatar nick commented on March 28, 2024

That last api looks good to me. In the meanwhile I'll try your suggestion. Can't wait to see what @ryanflorence and @mjackson do with support in react-router! Already using it in all my other projects.

from react-native.

ReadingSteiner avatar ReadingSteiner commented on March 28, 2024

@ericvicenti
Thanks a lot for ur reply :)

from react-native.

backslash112 avatar backslash112 commented on March 28, 2024

@nick
Checkout this demo: react-native_flux_demo

from react-native.

tbolt avatar tbolt commented on March 28, 2024

Flux being the only solution for this isn't ideal.

from react-native.

backslash112 avatar backslash112 commented on March 28, 2024

@teebolt
I agree with you, because I met a new problem when I trying to use Flux:
I created a TodoItems app too, When I succeeded in pushing a notification to the child component, and the child component will save the new item to the db, after this, the child component will push another notification to the list component for reload the list, the problem is, there are two dispatches in this process and it will cause a error: Invariant Violation: Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch.

from react-native.

tbolt avatar tbolt commented on March 28, 2024

I was able to solve this using a simple event emitter. I can post more details if anyone needs.

from react-native.

backslash112 avatar backslash112 commented on March 28, 2024

@teebolt Please post more details about simple event emitter, thank you.

from react-native.

backslash112 avatar backslash112 commented on March 28, 2024

@teebolt
Thank you very much, it works.
But if you want to start another dispatch in a listener that is triggered by emitChange() of a Store, you will get the same error like I said above: Invariant Violation: Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch.

from react-native.

chetankothari avatar chetankothari commented on March 28, 2024

@ericvicenti I tried the approach you mentioned in one of the replies, thanks for the detailed explanation. Just wanted to check if the same holds true now or is there another way of solving it in a better way. If so could you please help me out with that.

Thanks.

from react-native.

ericvicenti avatar ericvicenti commented on March 28, 2024

These days I would just go with a redux-style control flow, but its not too easy to pass props down into the inside of NavigatorIOS scenes. But it can probably be done with a redux provider, and that would be a much cleaner solution

from react-native.

chetankothari avatar chetankothari commented on March 28, 2024

@ericvicenti Thanks for the prompt response. Will try out getting redux-style control flow.

from react-native.

littlehome-eugene avatar littlehome-eugene commented on March 28, 2024

@ericvicenti @chetankothari
Can you elaborate what you mean by redux-style control flow?

Notify the click event through redux state?

from react-native.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.