• /
  • Log in
  • Free account

Persist the selected version

Course

This lesson is part of a course that teaches you how to build a New Relic One application from the ground up. If you haven't already, check out the course introduction.

Each lesson in the course builds upon the last, so make sure you've completed the last lesson, Add a section to end your test, before starting this one.

In the previous lesson, you created a section of your application that allows you to pick the most effective design from your A/B test. The goal of this section is to end the test once you’ve selected a winner:

First version of your end test section

When you select a version from this form, the selection does not persist in the Select component. Now, it’s time to implement that behavior.

Step 1 of 11

Change to the persist-selected-version directory of the coursework repository:

bash
$
cd nru-programmability-course/persist-selected-version
Step 2 of 11

In end-test.js, initialize EndTestSection.state with a default selectedVersion field:

import React from 'react';
import {
Button,
Grid,
GridItem,
HeadingText,
Select,
SelectItem,
} from 'nr1';
class VersionSelector extends React.Component {
render() {
return <Select>
<SelectItem value={'A'}>Version A</SelectItem>
<SelectItem value={'B'}>Version B</SelectItem>
</Select>
}
}
class EndTestButton extends React.Component {
render() {
return <div>
<Button>End test</Button>
</div>
}
}
export default class EndTestSection extends React.Component {
constructor() {
super(...arguments);
this.state = {
selectedVersion: 'A',
};
}
render() {
return <Grid className="endTestSection">
<GridItem columnSpan={12}>
<HeadingText className="endTestHeader">
Pick the winner of your A/B test:
</HeadingText>
</GridItem>
<GridItem columnStart={5} columnEnd={6} className="versionSelector">
<VersionSelector />
</GridItem>
<GridItem columnStart={7} columnEnd={8}>
<EndTestButton>End test</EndTestButton>
</GridItem>
</Grid>
}
}
nerdlets/ab-test-nerdlet/end-test.js
Step 3 of 11

Pass the selectedVersion, as a prop, to VersionSelector:

import React from 'react';
import {
Button,
Grid,
GridItem,
HeadingText,
Select,
SelectItem,
} from 'nr1';
class VersionSelector extends React.Component {
render() {
return <Select>
<SelectItem value={'A'}>Version A</SelectItem>
<SelectItem value={'B'}>Version B</SelectItem>
</Select>
}
}
class EndTestButton extends React.Component {
render() {
return <div>
<Button>End test</Button>
</div>
}
}
export default class EndTestSection extends React.Component {
constructor() {
super(...arguments);
this.state = {
selectedVersion: 'A',
};
}
render() {
return <Grid className="endTestSection">
<GridItem columnSpan={12}>
<HeadingText className="endTestHeader">
Pick the winner of your A/B test:
</HeadingText>
</GridItem>
<GridItem columnStart={5} columnEnd={6} className="versionSelector">
<VersionSelector
selectedVersion={this.state.selectedVersion}
/>
</GridItem>
<GridItem columnStart={7} columnEnd={8}>
<EndTestButton>End test</EndTestButton>
</GridItem>
</Grid>
}
}
nerdlets/ab-test-nerdlet/end-test.js
Step 4 of 11

Supply the selectedVersion to your Select component in VersionSelector:

import React from 'react';
import {
Button,
Grid,
GridItem,
HeadingText,
Select,
SelectItem,
} from 'nr1';
class VersionSelector extends React.Component {
constructor(props) {
super(props);
}
render() {
return <Select value={this.props.selectedVersion}>
<SelectItem value={'A'}>Version A</SelectItem>
<SelectItem value={'B'}>Version B</SelectItem>
</Select>
}
}
class EndTestButton extends React.Component {
render() {
return <div>
<Button>End test</Button>
</div>
}
}
export default class EndTestSection extends React.Component {
constructor() {
super(...arguments);
this.state = {
selectedVersion: 'A',
};
}
render() {
return <Grid className="endTestSection">
<GridItem columnSpan={12}>
<HeadingText className="endTestHeader">
Pick the winner of your A/B test:
</HeadingText>
</GridItem>
<GridItem columnStart={5} columnEnd={6} className="versionSelector">
<VersionSelector
selectedVersion={this.state.selectedVersion}
/>
</GridItem>
<GridItem columnStart={7} columnEnd={8}>
<EndTestButton>End test</EndTestButton>
</GridItem>
</Grid>
}
}
nerdlets/ab-test-nerdlet/end-test.js
Step 5 of 11

Create a method, selectVersion(), that updates EndTestSection.state:

import React from 'react';
import {
Button,
Grid,
GridItem,
HeadingText,
Select,
SelectItem,
} from 'nr1';
class VersionSelector extends React.Component {
constructor(props) {
super(props);
}
render() {
return <Select value={this.props.selectedVersion}>
<SelectItem value={'A'}>Version A</SelectItem>
<SelectItem value={'B'}>Version B</SelectItem>
</Select>
}
}
class EndTestButton extends React.Component {
render() {
return <div>
<Button>End test</Button>
</div>
}
}
export default class EndTestSection extends React.Component {
constructor() {
super(...arguments);
this.state = {
selectedVersion: 'A',
};
this.selectVersion = this.selectVersion.bind(this);
}
selectVersion(event, value) {
this.setState({ selectedVersion: value });
}
render() {
return <Grid className="endTestSection">
<GridItem columnSpan={12}>
<HeadingText className="endTestHeader">
Pick the winner of your A/B test:
</HeadingText>
</GridItem>
<GridItem columnStart={5} columnEnd={6} className="versionSelector">
<VersionSelector
selectedVersion={this.state.selectedVersion}
/>
</GridItem>
<GridItem columnStart={7} columnEnd={8}>
<EndTestButton>End test</EndTestButton>
</GridItem>
</Grid>
}
}
nerdlets/ab-test-nerdlet/end-test.js

You must bind selectVersion to the EndTestSection component so it can access state.

Step 6 of 11

Pass the method to VersionSelector as a prop:

import React from 'react';
import {
Button,
Grid,
GridItem,
HeadingText,
Select,
SelectItem,
} from 'nr1';
class VersionSelector extends React.Component {
constructor(props) {
super(props);
}
render() {
return <Select value={this.props.selectedVersion}>
<SelectItem value={'A'}>Version A</SelectItem>
<SelectItem value={'B'}>Version B</SelectItem>
</Select>
}
}
class EndTestButton extends React.Component {
render() {
return <div>
<Button>End test</Button>
</div>
}
}
export default class EndTestSection extends React.Component {
constructor() {
super(...arguments);
this.state = {
selectedVersion: 'A',
};
this.selectVersion = this.selectVersion.bind(this);
}
selectVersion(event, value) {
this.setState({ selectedVersion: value });
}
render() {
return <Grid className="endTestSection">
<GridItem columnSpan={12}>
<HeadingText className="endTestHeader">
Pick the winner of your A/B test:
</HeadingText>
</GridItem>
<GridItem columnStart={5} columnEnd={6} className="versionSelector">
<VersionSelector
selectedVersion={this.state.selectedVersion}
selectVersion={this.selectVersion}
/>
</GridItem>
<GridItem columnStart={7} columnEnd={8}>
<EndTestButton>End test</EndTestButton>
</GridItem>
</Grid>
}
}
nerdlets/ab-test-nerdlet/end-test.js
Step 7 of 11

Set selectVersion as the onChange callback in VersionSelector:

import React from 'react';
import {
Button,
Grid,
GridItem,
HeadingText,
Select,
SelectItem,
} from 'nr1';
class VersionSelector extends React.Component {
constructor(props) {
super(props);
}
render() {
return <Select onChange={this.props.selectVersion} value={this.props.selectedVersion}>
<SelectItem value={'A'}>Version A</SelectItem>
<SelectItem value={'B'}>Version B</SelectItem>
</Select>
}
}
class EndTestButton extends React.Component {
render() {
return <div>
<Button>End test</Button>
</div>
}
}
export default class EndTestSection extends React.Component {
constructor() {
super(...arguments);
this.state = {
selectedVersion: 'A',
};
this.selectVersion = this.selectVersion.bind(this);
}
selectVersion(event, value) {
this.setState({ selectedVersion: value });
}
render() {
return <Grid className="endTestSection">
<GridItem columnSpan={12}>
<HeadingText className="endTestHeader">
Pick the winner of your A/B test:
</HeadingText>
</GridItem>
<GridItem columnStart={5} columnEnd={6} className="versionSelector">
<VersionSelector
selectedVersion={this.state.selectedVersion}
selectVersion={this.selectVersion}
/>
</GridItem>
<GridItem columnStart={7} columnEnd={8}>
<EndTestButton>End test</EndTestButton>
</GridItem>
</Grid>
}
}
nerdlets/ab-test-nerdlet/end-test.js
Step 8 of 11

Navigate to the root of your Nerdpack at nru-programmability-course/persist-selected-version/ab-test.

Step 9 of 11

Generate a new UUID for your Nerdpack:

bash
$
nr1 nerdpack:uuid -gf

Because you cloned the coursework repository that contained an existing Nerdpack, you need to generate your own unique identifier. This UUID maps your Nerdpack to your New Relic account.

Step 10 of 11

Serve your application locally:

bash
$
nr1 nerdpack:serve
Step 11 of 11

View your changes:

Your choice persists in the version selector

When you're finished, stop serving your New Relic One application by pressing CTRL+C in the terminal window where you're serving your application.

Voila! When you select a new version as the winner of the A/B test, that version is reflected in the menu. However, when you press End test, nothing happens. In the next lesson, you'll create a confirmation dialog to protect yourself from prematurely ending your A/B test.

Course

This lesson is part of a course that teaches you how to build a New Relic One application from the ground up. Continue on to the next lesson: Present an end test confirmation modal.

Create issueEdit page
Copyright © 2021 New Relic Inc.