react-router icon indicating copy to clipboard operation
react-router copied to clipboard

passing state passes it to all Handlers

Open thomasmattheussen opened this issue 11 years ago • 3 comments

I went from this

render: function() {
        return (
            <div className="wrapper">
                <Header />
                <Pages>
                    <Page active pageId="newmessage">
                        <MessagePost />
                    </Page>
                    <Page pageId="allmessages">
                        <Feed messages={this.state.messages} />
                    </Page>
                    <Page pageId="searchmessages">
                        <MessageSearch />
                    </Page>
                </Pages>
            </div>
        );
    }

to this (now using react-router)

render: function() {
        return (
            <div className="wrapper">
                <Header />
                <Pages>
                    <Page>
                        <RouteHandler {...this.state} />
                    </Page>
                </Pages>
            </div>
        );
    }

I read that passing state should supposedly be done this way...

A question I started asking myself was that I now transfer the state to ALL pages instead of just to . Wouldn't that be considered some sort of violation against separation principles? Since and have no business with this.state.messages, they probably also shouldn't know about it?

Or am I talking gibberish here? :)

thomasmattheussen avatar Feb 25 '15 11:02 thomasmattheussen

<RouteHandler {...this.state} />

This doesn't make much sense in my opinion. You've probably seen this:

router.run(function(Handler, state){
  React.render(<Handler {...state} />, document.body);
});

Here, we're simply passing the current router state object as props to the top level route handler. It's not much different from writing <Handler routeState={state} />, except that you can simply write this.props.params instead of this.props.routeState.params to get the current params inside the handler.

As for your problem with passing the right data to lower level route handlers, you could try to make them work independent from each other and extract the logic to manage state.messages, e.g. using flux. Then all your route handlers would simply read from that store and trigger actions. I'm not sure if that's much better than <RouteHandler messages={this.state.messages} /> though, but it would allow you to fetch messages async before render.

agundermann avatar Mar 04 '15 19:03 agundermann

Closing this since its just an app design question, but happy to keep discussing.

@thomasmattheussen if feed is the only view that cares about the messages, why does it not own that state?

ryanflorence avatar Mar 25 '15 19:03 ryanflorence

Hmm, good question.

I think I made that decision because I am using feed somewhere else in the app in a slightly different context, so I wanted to make Feed as simple as possible: just a component that displays messages in a list.

A little more in depth:

I am using Feed as a list of all messages in my app. But I am also using a Feed component beneath a search-input. Both display a list of messages, but the first one gets updated whenever a new message is posted, the latter displays all messages based on a search-query.

So that's why in my point of view the logic for both lives one level higher.

Does that make sense? :)

thomasmattheussen avatar Mar 26 '15 16:03 thomasmattheussen