Best pattern for a 'Save' button in the header
Often times if you are editing or creating something, one of the header buttons will be 'Save'. I don't see how this is feasible with this library since there is no access to the state or props of the current screen component. The only (ugly) solution would be to keep the data to be saved outside of the component. Anyone have any suggestions how this pattern can be achieved with this library?
On a somewhat related note, Ive seen references to a Slack channel. That would be a better place for this discussion but I'm unsure of how to get access to that channel.
There is some discussion on the reactiflux discord server (https://www.reactiflux.com/), specifically in the rn-navigation channel, although I don't think its specifically for this library.
Related: #149
the only solution I can think of is using setParams to set the callback/state in the controller and using state.params in the header
Thanks @Schniz . Thats the pattern I was thinking as well and the path that I ultimately took. Unfortunately I ran into what appears to be a bug related to calling setParams. Ive opened a separate issue for that bug. All in all, this pattern seems a bit hazy and not entirely 'safe' unless you explicitly check for the existence of all state.params.
Yeah. I think that state.params should default to an empty object.
However, I really dig using ramda's pathOr for these kind of problems.
import { pathOr } from 'ramda';
const noop = () => {};
// ...
const somethingToDoFn = pathOr(noop, ['params', 'somethingToDoFn'], navigator);
somethingToDoFn('data', 'to', 'pass');
Isn't the proper way to do this:
componentDidMount () {
const isDisabled = <someLogicUsingLocalState>;
const params = {
right: (
<Button
onPress={() =>!isDisabled ? this.onSave() : null}
>
<Text style={[styles.rightButtonTextStyle, isDisabled ? styles.disabledStyle : null]}>
Save
</Text>
</Button>
),
};
this.props.navigation.setParams(params);
}
However, https://github.com/react-community/react-navigation/issues/160 is preventing this from working well.
Thought I would post my main navigation options too for completness sake:
function getCurrentParams (state) {
if (state.routes) {
return getCurrentParams(state.routes[state.index]);
}
return state.params || {};
}
...some setup...
// main stack navigator navigationOptions
navigationOptions: {
header: ({state}) => {
// get the "deepest" current params.
const currentParams = getCurrentParams(state);
const left = currentParams.left;
const right = currentParams.right;
const style = currentParams.style;
const tintColor = currentParams.tintColor;
return { left, right, style, tintColor };
}
}
This feels kinda hacky to me, but I don't see another way to set dynamic header properties other than the use of setParams (and again, https://github.com/react-community/react-navigation/issues/160 makes this a poor solution at the moment).
Given that #160 is already fixed, do you think @dmhood your solution can become more standardised? Do you think it deserves a doc How to just for the sake of avoiding these questions in the future?
If people find that useful then I'll make a pr here in the next couple of days, and we can standardize/critique it there.
@dmhood solution works great. I was looking to show a modal view when pressing one of the header buttons (left or right). Thanks!
FWIW using setParams in componentDidMount is apparently discouraged, (see #296). I've mostly gone to pulling most of the "initial state" logic to set header buttons out into static/helper functions, which isn't great but should be doable unless your component uses something external like react-context.
I'm not sure I understood @dmhood's solution. How do I call the saveDetails() function? This code throws error, by the way: "_this2.saveDetails is not a function". Help would be appreciated. Thanks!
class MyScreen extends React.Component {
static navigationOptions = {
header: {
right: <Button title={"Save"} onPress={() => this.saveDetails()} />
}
};
saveDetails() {
alert('Save Details');
}
render() {
return (
<View />
);
}
}
@raymundtusk7 you can't call this.saveDetails() from a static context since the actual saveDetails method is a class method. You would need to make saveDetails static or just some helper function outside of the class.
Maybe all the functions (when declaring navigationOptions as a function for instance) should receive the actual component as an argument
Can't seem to wrap my head around this. If I make the saveDetails static, would it have access to the screen's state object?
Another scenario: Let's say I have a login screen with 2 text inputs and a login button at the header's right component. How would it send the details to the REST API via fetch?
Try setting your component instance's handleSave function as a navigation state parameter, after the component has mounted. Something like:
class MyScreen extends React.Component {
static navigationOptions = {
header: ({ state }) => ({
right: <Button title={"Save"} onPress={state.params.handleSave} />
})
};
saveDetails() {
alert('Save Details');
}
componentDidMount() {
this.props.navigation.setParams({ handleSave: this.saveDetails });
}
render() {
return (
<View />
);
}
}
@stevehollaar: Awesome! This worked for me though:
<Button title={"Save"} onPress={() => {state.params.handleSave()}} />
But you pointed me to the right direction. Thanks a lot! 💯
Is there a way to dynamically enable or disable it? Meaning, if the user changes input the Save button would be enabled