Skip to content

Implement two-pass render (closes #81) #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 54 additions & 19 deletions modules/Media.js
Original file line number Diff line number Diff line change
@@ -24,31 +24,51 @@ class Media extends React.Component {

queries = [];

state = {
matches:
this.props.defaultMatches ||
Object.keys(this.props.queries).reduce(
(acc, key) => ({ ...acc, [key]: true }),
{}
)
};
constructor(props) {
super(props);

if (typeof window !== "object") {
// In case we're rendering on the server
this.state = {
matches:
this.props.defaultMatches ||
Object.keys(this.props.queries).reduce(
(acc, key) => ({ ...acc, [key]: true }),
{}
)
};
return;
}

updateMatches = () => {
const newMatches = this.queries.reduce(
this.initialize();

// Instead of calling this.updateMatches, we manually set the state to prevent
// calling setState, which could trigger an unnecessary second render
this.state = {
matches:
this.props.defaultMatches !== undefined
? this.props.defaultMatches
: this.getMatches()
};
this.onChange();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure whether we should be calling onChange in the constructor. I added it now because this is in line with the current behavior.

}

getMatches = () => {
return this.queries.reduce(
(acc, { name, mqList }) => ({ ...acc, [name]: mqList.matches }),
{}
);
this.setState({ matches: newMatches });

const { onChange } = this.props;
if (onChange) {
onChange(newMatches);
}
};

componentWillMount() {
if (typeof window !== "object") return;
updateMatches = () => {
const newMatches = this.getMatches();

this.setState(() => ({
matches: newMatches
}), this.onChange);
};

initialize() {
const targetWindow = this.props.targetWindow || window;

invariant(
@@ -67,8 +87,23 @@ class Media extends React.Component {

return { name, qs, mqList };
});
}

this.updateMatches();
componentDidMount() {
this.initialize();
// If props.defaultMatches has been set, ensure we trigger a two-pass render.
// This is useful for SSR with mismatching defaultMatches vs actual matches from window.matchMedia
// Details: https://github.com/ReactTraining/react-media/issues/81
if (this.props.defaultMatches !== undefined) {
this.updateMatches();
}
}

onChange() {
const { onChange } = this.props;
if (onChange) {
onChange(this.state.matches);
}
}

componentWillUnmount() {
16 changes: 16 additions & 0 deletions modules/__tests__/Media-test.js
Original file line number Diff line number Diff line change
@@ -291,4 +291,20 @@ describe("A <Media> in browser environment", () => {
});
});
});

describe("when defaultMatches have been passed", () => {
beforeEach(() => {
window.matchMedia = createMockMediaMatcher(false);
});

it("initially overwrites defaultMatches with matches from matchMedia", async () => {
const element = <Media queries={{ matches: "" }} defaultMatches={{ matches: true }}>
{({ matches }) => matches ? <div>fully matched</div> : <div>not matched</div>}
</Media>;

ReactDOM.render(element, node, () => {
expect(node.firstChild.innerHTML).toMatch('not matched');
});
});
})
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@
"eslint-plugin-jest": "^20.0.3",
"eslint-plugin-react": "^6.0.0",
"gzip-size": "^3.0.0",
"jest": "^20.0.4",
"jest": "^23.5.0",
"pascal-case": "^2.0.1",
"pretty-bytes": "^4.0.2",
"react": "^15.4.1 || ^0.14.7",