kriasoft / react-app

Create React App with server-side code support
https://t.me/reactapp
MIT License
613 stars 84 forks source link

Give the context function the location as an argument when running in the browser #2

Closed dj311 closed 7 years ago

dj311 commented 8 years ago

In the browser createApp function, if a context generating function is passed, it will be given the new location descriptor as an argument whenever history.listen detects a change.

This mirrors the passing of the Node request object into the context generating function on the server side.

This means we can create context generating functions on the client-side (based on the location object) and the server-side (based on the Node request). These functions can perform the task of normalising the context presented to the application.

As an example use case, this allows integration with react-router-form (an isomorphic/universal form component). This form component handles submitting of the form in two ways:

We can then make context normalising functions on the client and server to give a consistent view of the request to the application:

let client_context = function(loc) {
    return {
        request: {
            method: (loc.state === null) ? 'GET' : loc.state.method,
            body:   (loc.state === null) ? {}    : loc.state.body
        }
    }
}
let server_context = function(req) {
    return {
        request: {
            method: req.method || 'GET',
            body: req.body || {}
        }
    }
}

I think this change is general enough to be useful for a variety of other use cases. And presents a consistent interface (presenting request on the server-side, and it's analogue location on the client-side).

Thanks, Dan

dj311 commented 8 years ago

(The failing build seems to be unrelated - giving the same errors as Build #13)