Comments (20)
@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.
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.
@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.
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.
btw I'd love to contribute this example to the official repo once it's done :-)
from react-native.
You are right, we need to have a todo app example otherwise we're not a real js lib :p
from react-native.
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.
@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.
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.
@ericvicenti
Thanks a lot for ur reply :)
from react-native.
@nick
Checkout this demo: react-native_flux_demo
from react-native.
Flux being the only solution for this isn't ideal.
from react-native.
@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.
I was able to solve this using a simple event emitter. I can post more details if anyone needs.
from react-native.
@teebolt Please post more details about simple event emitter, thank you.
from react-native.
@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.
@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.
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.
@ericvicenti Thanks for the prompt response. Will try out getting redux-style control flow.
from react-native.
@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)
- Horrible experience starting a project HOT 3
- Build failing after upgrading react-native to 0.74.1 HOT 3
- Layout inspector shows wrong frames when using `DrawerLayoutAndroid` HOT 1
- Task :app:checkReleaseDuplicateClasses FAILED HOT 8
- Priavacy Policy aggregation errors on get_privacyinfo_file_path HOT 9
- building android build for the react native tester app is failing HOT 9
- Getting error when changing parenthesis position HOT 3
- Execution failed for task ':app:compileDebugKotlin'. HOT 6
- Xcode works normally during real machine debugging, but when disconnected, clicking on the app to open will crash. The mobile system is 13.8. No problem with systems above 14 HOT 6
- cannot get 304 Not modified status from fetch
- Android build fail - Unable to make progress running work. There are items queued for execution but none of them can be started HOT 13
- redefinition of module 'ReactCommon' iOS HOT 7
- java.lang.RuntimeException: com.facebook.react.devsupport.JSException: Cannot read property 'setGlobalHandler' of undefined HOT 6
- Buttons are not clickable until the TextInputField receives focus. HOT 3
- Performance Issues with useNativeDriver in React Native 0.74.1 of New Arch: Comparing Animation Smoothness and FlatList Scrolling HOT 2
- Appreance.getColorScheme() always returns "light" | Android | IOS | React Native | Expo HOT 2
- Command PhaseScriptExecution failed with a nonzero exit code with ios build HOT 6
- Unable to add private maven repo to the React native project directly HOT 6
- Using initialScrollIndex with Flatlist will cause missing items with react native HOT 1
- Unexpected keyword 'typeof' HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native.