JavaScript JSON object vs window variable for passing server-side rendered initial state using reactjs json.stringify window object,pass json object in window.location.

In an isomorphic application with Reactjs, you need to pass the identical initial state that was rendered on the server down to the client (which will then 'rehydrate' the app with event bindings and such).

I've seen two approaches to passing this initial state down--

Setting a global variable to the window:

<script>
window.initialState = {{JSON.stringify(initialState)}} ;
</script>

Or passing it as a JSON object:

<script id="initial-state" type="application/json"> {{JSON.stringify(initialState)}}</script>

Both are easily retrievable from anywhere in the application. Are there any advantages to using one over the other?

Answer:1

The latter avoids a global variable and the former avoids a DOM lookup. I'd go with the former, just because it requires less code.

One concern is if you have </script in your JSON it could allow injection or accidental error. To prevent this you can replace < with \u003c.

<script>
window.initialState = {{
    JSON.stringify(initialState).replace(/</g, '\\u003c')
}}; 
</script>
Answer:2

I have a javascript object and want to use angular's filter on a specific deep property but it does not seem to work. function MyCtrl($scope, $filter) { $scope.items = [ {id:{val:1,str:'abc'}, ...

I have a javascript object and want to use angular's filter on a specific deep property but it does not seem to work. function MyCtrl($scope, $filter) { $scope.items = [ {id:{val:1,str:'abc'}, ...

  1. angularjs filter specific fields
  2. angularjs filter specific property
  3. angularjs filter specific column
  4. angularjs filter by property
  5. angularjs filter by multiple properties
  6. angularjs filter by object property
  7. angularjs filter by date
  8. angularjs filter by
  9. angularjs filter by name
  10. angularjs filter by field
  11. angularjs filter by property value
  12. angularjs filter by function
  13. angularjs filter by array of values
  14. angularjs filter by multiple values
  15. angularjs filter by value
  16. angularjs filter by id
  17. angularjs filter by key
  18. angularjs filter by two properties
  19. angularjs filter by array
  20. angularjs filter by property in controller

I am trying to develope a e-learning site for kids. It contains an excercise to select the right answer among the given four options,by seeing the clue image. When the kid selects the right answer,the ...

I am trying to develope a e-learning site for kids. It contains an excercise to select the right answer among the given four options,by seeing the clue image. When the kid selects the right answer,the ...

  1. javascript validation error message display
  2. javascript validation error
  3. javascript validation error in eclipse
  4. javascript validation error messages
  5. javascript validation error class
  6. javascript throw validation error
  7. javascript add validation error
  8. javascript remove validation error
  9. javascript show validation error
  10. javascript form validation error div
  11. javascript form validation error messages
  12. javascript html validation error
  13. javascript internal validation error
  14. javascript checkbox validation error
  15. javascript validation remove error message
  16. javascript input error validation
  17. javascript focus on validation error
  18. javascript show validation error message
  19. javascript custom validation error message
  20. javascript input validation error message

I'm using Cors requests to communicate between my client and server placed in two different domain. I configurate my apache http server this way it's using SSL : //with AJAX withCredentials=true (...

I'm using Cors requests to communicate between my client and server placed in two different domain. I configurate my apache http server this way it's using SSL : //with AJAX withCredentials=true (...

  1. cors request method options
  2. cors access-control-request-method
  3. invalid cors request post method

Im using dropzonejs to upload photos to my website. The problem is, when I click dropzone to upload, it won't show 'Gallery' as an option as seen in this image : How to add Gallery as an option?

Im using dropzonejs to upload photos to my website. The problem is, when I click dropzone to upload, it won't show 'Gallery' as an option as seen in this image : How to add Gallery as an option?

  1. upload photo from iphone
  2. upload photo from iphone to pc
  3. upload photo from iphone to icloud
  4. upload photo from phone to pc
  5. upload photo from desktop to instagram
  6. upload photo from computer to iphone
  7. upload photo from pc to instagram
  8. upload photo from iphone to google drive
  9. upload photo from iphone to dropbox
  10. upload photo from pc to icloud
  11. upload photo from computer to instagram
  12. upload photo from iphone to facebook
  13. upload photo from mac to iphone
  14. upload photo from camera roll to snapchat
  15. upload photo from instagram web
  16. upload photo from gallery android studio
  17. upload photo from iphone to laptop
  18. upload photo from mac to instagram
  19. upload photo from phone to laptop
  20. upload photo from mac