isomorphic-javascript

React 16: Warning: Expected server HTML to contain a matching <div> in <div> due to State

百般思念 提交于 2020-08-24 06:03:21
问题 I'm getting the following error using SSR Warning: Expected server HTML to contain a matching <div> in <div>. The issue is on the client when checking the browser width on component mount, and then setting the state of a component to render a mobile version of it instead. But the server is defaulting the desktop version of the container as it is not aware of the browser width. How do I deal with such a case? Can I somehow detect the browser width on the server and render the mobile container

React 16: Warning: Expected server HTML to contain a matching <div> in <div> due to State

試著忘記壹切 提交于 2020-08-24 05:58:27
问题 I'm getting the following error using SSR Warning: Expected server HTML to contain a matching <div> in <div>. The issue is on the client when checking the browser width on component mount, and then setting the state of a component to render a mobile version of it instead. But the server is defaulting the desktop version of the container as it is not aware of the browser width. How do I deal with such a case? Can I somehow detect the browser width on the server and render the mobile container

How to avoid getting error 'localStorage is not defined' on server in ReactJS isomorphic app?

二次信任 提交于 2020-06-06 06:17:48
问题 I was using localStorage in my webApp to store data on client side. But when I've tried to make the app isomorphic, this causes an issue. Due to node is not browser environment, it can't define such objects as 'window', 'localStorage' etc. How do I solve this issue? 回答1: You could use a check whether the code is being executed on the server or on the client side by checking whether module is not 'undefined' : var isNode = typeof module !== 'undefined' You can then proceed to only executed

How to avoid getting error 'localStorage is not defined' on server in ReactJS isomorphic app?

左心房为你撑大大i 提交于 2020-06-06 06:15:56
问题 I was using localStorage in my webApp to store data on client side. But when I've tried to make the app isomorphic, this causes an issue. Due to node is not browser environment, it can't define such objects as 'window', 'localStorage' etc. How do I solve this issue? 回答1: You could use a check whether the code is being executed on the server or on the client side by checking whether module is not 'undefined' : var isNode = typeof module !== 'undefined' You can then proceed to only executed

How can I set window to undefined in order to test the SSR rendering in an isomorphic application?

淺唱寂寞╮ 提交于 2020-03-03 12:15:14
问题 I have been trying to test an else block for when window is undefined. As I'm using Next.js the window will be undefined during server side rendering (SSR). At present I can not find a way to do this. Any help is much appreciated. I have tried setting window = undefined and global.window = undefined in the test and within the definition before function (both all and each options). Both approaches have not been successful. When Googling the closest answer I can find is mocking or spying on a

Doing a PATCH with a file and some fields using multipart/form-data

↘锁芯ラ 提交于 2020-01-25 04:20:10
问题 I am doing a request to my server to patch some regular form fields as well as upload an image. If I do these alone in separate requests all works perfectly, but nothing works if the form data and the file are in the same request. This is what it currently looks like: const file = new FormData() file.append('uploadedFile', uploadedFile, uploadedFile.name) const patch = { name: 'Some Name', age: 18, file } return isomorphicFetch(`${MY_URL}`, { headers: { Accept: 'application/json' } method:

react.js don't render until ajax request finish

喜欢而已 提交于 2020-01-24 08:59:15
问题 I have a pretty simple React.js component which I need to make an isomorphic (rendered on the server). The problem is that component rendered with helpful information only after ajax request completes, like that: export default React.createClass({ getInitialState() { return {} }, componentDidMount() { fetch("/users/").then(response => { this.setState(users: response.data) }) }, render() { if (this.state.users == undefined) { return <div /> } return <div>{this.state.users.map(some_function)}<

How to import CSS file on Isomorphic React - Webpack

旧时模样 提交于 2020-01-23 08:11:07
问题 I'm trying to create an isomorphic react app using express, react, and webpack. Everything works fine until I import a css file in one of my components. I understand node can not handle this import, but can someone explain how this package on github allows their components to have a css import line? https://github.com/kriasoft/react-starter-kit I would like to make my project similar to that. Do they have a line anywhere that has the server ignore that line when rendering components? This is

How to import CSS file on Isomorphic React - Webpack

不羁的心 提交于 2020-01-23 08:10:53
问题 I'm trying to create an isomorphic react app using express, react, and webpack. Everything works fine until I import a css file in one of my components. I understand node can not handle this import, but can someone explain how this package on github allows their components to have a css import line? https://github.com/kriasoft/react-starter-kit I would like to make my project similar to that. Do they have a line anywhere that has the server ignore that line when rendering components? This is