JSX Whispers
As of LDK v3.10.0, you no longer need to install React and React-related dependencies to use JSX Whispers.

JSX Whispers

JSX is a syntax extension of JavaScript introduced by the team that created React. For the purposes of Whispers, there are a few things we leverage.
  1. 1.
    Properties can be defined more like an HTML tag, where components look something like <oh-component propertyOne="x" propertyTwo="y" />
  2. 2.
    React state is used to propagate changes to the different components, meaning that you don't have to explicitly use the update() function of the Whisper object.
Using JSX Whispers is a great way to have an easier time with handling updates in whispers.
The entry point for this webpack config is index.tsx. You may need to update your build command (or this code) in order to compile properly.
1
// webpack.config.js
2
const path = require('path');
3
const merge = require('webpack-merge');
4
const ldkConfig = require('@oliveai/ldk/dist/webpack/config');
5
6
const merged = merge.merge(ldkConfig.default, {
7
entry: [path.resolve(__dirname, './src/index.tsx')],
8
});
9
10
module.exports = merged;
Copied!
And with that in place, you're ready to try them out! Let's create a simple form that has a text input that we validate the value of. Using JSX, it would look something like this:
In the below code, the ReactWhisper object is used to render the JSX Whisper. Your Loop will have errors at runtime if you attempt to use it without either importing and using an aptitude or importing the LDK package as shown below.
1
// index.tsx
2
import { React, ReactWhisper } from '@oliveai/ldk';
3
import { StateMap } from '@oliveai/ldk/dist/whisper/types';
4
5
const ComponentDemo: React.FunctionComponent<Object> = (props) => {
6
const textInputId = 'someUniqueId';
7
const [validateButtonClicked, updateButtonClicked] = React.useState(false);
8
const [validationError, updateValidationError] = React.useState('');
9
10
const onChange = () => {
11
/* do nothing */
12
};
13
const onValidate = (state: StateMap) => {
14
updateValidationError(
15
state.get(textInputId) !== 'valid' ? "Input should be 'valid'" : undefined,
16
);
17
};
18
19
return (
20
<oh-whisper label="Simple Form" onClose={() => {}}>
21
<>
22
<oh-text-input
23
label="Input 'valid'"
24
id={textInputId}
25
key={textInputId}
26
onChange={onChange}
27
validationError={validationError}
28
/>
29
<oh-button
30
label={validateButtonClicked ? 'Validate Again' : 'Validate'}
31
onClick={(error, whisperProps) => {
32
updateButtonClicked(true);
33
onValidate(whisperProps.componentState);
34
}}
35
/>
36
</>
37
</oh-whisper>
38
);
39
};
40
41
ReactWhisper.renderNewWhisper(<ComponentDemo/>);
Copied!
This will display a Whisper that looks like this:
If we didn't put the word 'valid' in the Whisper, and then we press the 'Validate' button, the Whisper would now look like this:
Demonstrating that the updates we wanted to make to our Whisper are occurring, with much cleaner code than the prior examples. But, you aren't limited to just using JSX Whispers to only when you need to leverage Whisper updates! They can be used to display static content as well.
Copy link
Contents
JSX Whispers