In the official docs, it says, React will attempt to attach event listeners to the existing markup.
So I thought that if I use ReactDOM.hydrate, it'll only add events.
// server.js
app.get('/', (req, res) => {
const html = fs.readFileSync(path.resolve('./build/index.html'), 'utf8');
return res.send(
html.replace(
'<div id="root"></div>',
'<div id="root">hihi</div>',
),
);
}
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.hydrate(
<App/>,
document.getElementById('root'),
);
// App.jsx
import React from 'react';
import styled from 'styled-components';
export default () => {
return <Title>Hi !</Title>;
};
const Title = styled.h1`
color: blue;
`;
However when the hydrate works, it removes DOM created by SSR and replaces it with DOM created by CSR(react script)
Why is this happening?