Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about Actions.refresh #2089

Closed
compojoom opened this issue Jul 22, 2017 · 5 comments
Closed

Question about Actions.refresh #2089

compojoom opened this issue Jul 22, 2017 · 5 comments

Comments

@compojoom
Copy link
Contributor

I'm not sure if this was working the same on 3.x, but on 4 I noticed the following thing.

I have 3 scenes
A -> B -> C

Scene A is a list view.
Scene B shows a single Item
Scene C is an edit scene.

on Scene B I have this code:

	componentWillReceiveProps(nextProps) {
		if (this.props.company !== nextProps.company) {
			this.handleNavBar(nextProps.company);
		}
	}

	componentWillMount() {
		this.handleNavBar(this.props.company);
	}

	handleNavBar(company) {
        Actions.refresh({
            title: company.name,
            rightTitle: 'Bearbeiten',
            onRight: () => {
                this.props.editCompany(company.company_id);
                Actions.formCompany();
            }
        });
	}

Basically when the component is mounted we set a new title and we specify what should happen when you click the right button. This works fine. ComponentWillReceiveProps make sure that whenever our redux state is updated we update the Navbar header. That works.

However in Scene C I have this:

    componentDidMount() {
        let title = 'Betrieb hinzufügen',
            rightTitle = 'Speichern';

        if (this.props.editMode) {
            title = 'Betrieb Bearbeiten';
            rightTitle = 'Fertig';
        }
        Actions.refresh({
            title: title,
            rightTitle: rightTitle,
            onRight: () => this.onRight(),
            backTitle: 'Abbrechen'
        });
    }

I'm again modifiy the tilte and right button. The problem is that in the edit Scene I do an update to the company. When I do this scene's B componentWillreceiveProps is updated with the new company. the handleNavBar function is called and we call Actions.refresh on scene B. But it doesn't refresh the navbar on Scene B, but on Scene C.

So my title changes to "Company name" and my right button changed to "edit" and not "save"... Obviously the action is also different.

Is this the expected behavior and is there any way around this other than using my own Navbar or just not updating the on componentWillReceiveProps?

@aksonov
Copy link
Owner

aksonov commented Jul 25, 2017

refresh within componentDidMount or componentWillReceiveProps is definitely anti-pattern. All you need is Redux or MobX and change state of your app accordingly and define static onRight, rightTitle, title functions within your component that will display that state. For MobX, RNRF allows wrapping all navbar by 'observer', so it will be fully dynamic - i.e. updated automatically.

@compojoom
Copy link
Contributor Author

ok, Do you have an example?
I remember trying the static onRight in the past, but the issue was that I didn't have access to my component save function as this was not pointing to the component within the static function

@aksonov
Copy link
Owner

aksonov commented Jul 25, 2017

Yes, it is good question, it would be great to create separate issue for this - 'access to the component props from the navbar' I will think how to achieve that.

@aksonov aksonov closed this as completed Jul 25, 2017
@tendolukwago
Copy link

@aksonov going to start an issue specifically based off this.

@alfonsosn
Copy link

alfonsosn commented Apr 24, 2018

Any progress on this issue? I found this answer helpful: 1516

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants