Edit This Page

.setContext(context) => Self

A method that sets the context of the root component, and re-renders. Useful for when you are wanting to test how the component behaves over time with changing contexts.

NOTE: can only be called on a wrapper instance that is also the root instance.


  1. context (Object): An object containing new props to merge in with the current state


ShallowWrapper: Returns itself.


const SimpleComponent = React.createClass({
  contextTypes: {
    name: React.PropTypes.string,
  render() {
    return <div>{this.context.name}</div>;
const context = { name: 'foo' };
const wrapper = shallow(<SimpleComponent />, { context });
wrapper.setContext({ name: 'bar' });
wrapper.setContext({ name: 'baz' });

Common Gotchas

  • .setContext() can only be used on a wrapper that was initially created with a call to shallow() that includes a context specified in the options argument.
  • The root component you are rendering must have a contextTypes static property.