Go back to your project in the code editor.
If you’ve closed the code editor since the last section, remember that you can see and open your projects from the Developer Portal.
You can use the @withkoji/core package to determine the initial context that is loaded when your project is viewed by a user.
Open frontend/src/App.js
and update it to look like the following.
import Koji from '@withkoji/core';
import { useState } from 'react';
import './App.css';
function App() {
const [logoSrc, setLogoSrc] = useState('https://upload.wikimedia.org/wikipedia/commons/a/a7/React-icon.svg');
const { context } = Koji.playerState;
const captureImage = async () => {
const src = await Koji.ui.capture.image();
if (src) setLogoSrc(() => src);
};
if (context === 'remix') {
return (
<div className="App">
<header className="App-header">
<button onClick={captureImage}>Capture Image</button>
</header>
</div>
);
}
return (
<div className="App">
<header className="App-header">
<img src={logoSrc} className="App-logo" alt="logo" />
<p>Some New Text</p>
</header>
</div>
);
}
export default App;
You’ve added a number of things here.
First, you get a value from Koji.playerState.context
by using object destructuring.
The variable context
is set to a string value that represents the initial context.
In our current project, the value should be default
or remix
, depending on whether the user is consuming or remixing the template.
You’re also doing some conditional rendering.
If you’re in a remix
context, you want to show the button to capture an image.
If you’re not in a remix
context, you can assume that you’re in the default
context and show the rotating image, as well as some paragraph text.
Make sure to save your updates using Cmd+S
or Ctrl+S
, depending on your operating system.