received error message when using component - reason

I am creating a website using ReasonReact, but I encounter this error message when using a normal component. Does anyone know what is happening?
module Component1 = {
let component = ReasonReact.statelessComponent("Component1");
let make = () => {...component, render: self => <div />};
};
module Component2 = {
let component = ReasonReact.statelessComponent("Component1");
let make = () => {
...component,
render: self => <div> <Component1 /></div>, /*error on compenent1*/
};
Here is the error message:
(
React.component('props),
'props
) => React.element
<root>/node_modules/reason-react/src/React.re
Error: This expression has type
unit =>
ReasonReact.componentSpec(ReasonReact.stateless,
ReasonReact.stateless,
ReasonReact.noRetainedProps,
ReasonReact.noRetainedProps,
ReasonReact.actionless)
but an expression was expected of type
React.component(unit) = unit => React.element
Type
ReasonReact.componentSpec(ReasonReact.stateless,
ReasonReact.stateless,
ReasonReact.noRetainedProps,
ReasonReact.noRetainedProps,
ReasonReact.actionless)
is not compatible with type React.element

The problem seems to be that you're using a project configured to use JSX version 3 with components designed for JSX version 2.
JSX version 3 was introduced in ReasonReact 0.7.0, along with a new method for defining react components that supports hooks, but still supports the method you're using as long as you configure your project to use JSX version 2. If this is a new project, which it seems to be, I would recommend using the new component style, where your code would simply look like this:
module Component1 = {
[#react.component]
let make = () =>
<div />;
};
module Component2 = {
[#react.component]
let make = () =>
<div> <Component1 /> </div>;
};
Alternatively, you can continue using the old style of components and JSX version 2 by specifying the following in bsconfig.json:
{
...
"reason": {"react-jsx": 2}
}
See the blog post on ReasonReact 0.7.0 for more details.

Related

SvelteKit console error "window is not defined" when i import library

I would like to import apexChart library which using "window" property, and i get error in console.
[vite] Error when evaluating SSR module /src/routes/prehled.svelte:
ReferenceError: window is not defined
I tried use a apexCharts after mount, but the error did not disappear.
<script>
import ApexCharts from 'apexcharts'
import { onMount } from 'svelte'
const myOptions = {...myOptions}
onMount(() => {
const chart = new ApexCharts(document.querySelector('[data-chart="profit"]'), myOptions)
chart.render()
})
</script>
I tried import a apexCharts when i am sure that browser exist.
import { browser } from '$app/env'
if (browser) {
import ApexCharts from 'apexcharts'
}
But i got error "'import' and 'export' may only appear at the top level"
I tried disable ssr in svelte.config.js
import adapter from '#sveltejs/adapter-static';
const config = {
kit: {
adapter: adapter(),
prerender: {
enabled: false
},
ssr: false,
}
I tried to create a component in which I import apexChart library and I created a condition that uses this component only if a browser exists
{ #if browser }
<ProfitChart />
{ /if }
Nothing helped.
Does anyone know how to help me please?
The easiest way is to simply include apexcharts like a standalone library in your webpage like this:
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
And then simply use it in the onMount:
onMount(() => {
const chart = new ApexCharts(container, options)
chart.render()
})
You can add this line either in your app.html or include it where it's required with a <svelte:head> block.
An alternative way would be to dynamically import during onMount:
onMount(async () => {
const ApexCharts = (await import('apexcharts')).default
const chart = new ApexCharts(container, options)
chart.render()
})
As an extra: use bind:this instead of document.querySelector to get DOM elements, that would be the more 'svelte' way.
I have found the last option with the Vite plugin to work best with less code in the end but will lose intellisense in vscode and see import highlighted as error (temp workaround at end): https://kit.svelte.dev/faq#how-do-i-use-x-with-sveltekit-how-do-i-use-a-client-side-only-library-that-depends-on-document-or-window
Install vite plugin: npm i -D vite-plugin-iso-import
Add plugin to svelte.config.js:
kit: {
vite: {
plugins: [
isoImport(),
],
Add plugin to TypeScript config (if you use TS):
"compilerOptions": {
"plugins": [{ "name": "vite-plugin-iso-import" }],
Use as normal but note the "?client" on the import:
<script context="module">
import { chart } from 'svelte-apexcharts?client';
import { onMount } from 'svelte'
let myOptions = {...myOptions}
onMount(() => {
myOptions = {...updated options/data}
});
</script>
<div use:chart={myOptions} />
Debugging note:
To have import not highlighting as an error temporarily, just:
npm run dev, your project will compile fine, then test in browser to execute at least once.
remove ?client now, save and continue debugging as usual.
For all of you trying to import dynamically into a js or ts file, try the following:
Import your package during on mount in any svelte component.
onMount(async () => {
const Example = await import('#creator/examplePackage');
usePackageInJSOrTS(Example.default);
});
Use the imported package in your js/ts function. You need to pass the default value of the constructor.
export function usePackageInJsOrTs(NeededPackage) {
let neededPacakge = new NeededPackage();
}

StatusBar does not have web implementation

I'm trying to render a component using react-testing-library in an Ionic React based project. There appears to be an issue with StatusBar. The error says StatusBar does not have web implementation.
My code looks something like this:
let component
beforeEach(() => {
component = render(
<ThemeProvider>
<IonReactRouter>
<IonRouterOutlet>
<Login />
</IonRouterOutlet>
</IonReactRouter>
</ThemeProvider>
)
})
describe('snapshot', () => {
it('should match snapshot', () => {
const { asFragment } = component
expect(asFragment()).toMatchSnapshot()
})
})
That's no error, that's the Capacitor Plugin not having the Web Implementation, you could just ignore that or catch it everywhere with .catch(()=>{});
Have you installed #capacitor/status-bar in /src-capacitor? (yarn add #capacitor/status-bar or npm install ....)

Getting error 'variant constructor can't be found' while compiling

I was trying out use a sortable list component (react-sortable-hoc) on a ReasonReact project. But I ran into an error which I was trying to figure out for couple of hours.
Steps I followed:
made the #bs binds for sortableContainer() and sortableElement() for the module react-sortable-hoc
faked the reactClass returned by the both functions and put it under file SortableContainer.js and SortableElement.js
Made an another React component called Todolist which use the component SortableContainer, and SortableContainer use the component SortableElement.
Code Snippet
/* SortableContainer.re */
[#bs.val] [#bs.module "react-sortable-hoc"] external sortableContainer : 'a =>
ReasonReact.reactClass = "";
let sortableContainerReactClass: ReasonReact.reactClass = sortableContainer(() => {
(ReasonReact.createElement(SortableElement))
});
let make = (children) =>
ReasonReact.wrapJsForReason(
~reactClass=sortableContainerReactClass,
~props = { "onSortEnd": () => {} },
children
);
/* SortableElement.re */
[#bs.val] [#bs.module "react-sortable-hoc"] external sortableElement : 'a =>
ReasonReact.reactClass = "";
let sortableElementReactClass: ReasonReact.reactClass = sortableElement(() => {
(ReasonReact.stringToElement("testing is good"))
});
let make = (children) =>
ReasonReact.wrapJsForReason(
~reactClass=sortableElementReactClass,
~props = { "onSortEnd": () => {} },
children
);
/* List.re */
let component = ReasonReact.statelessComponent("List");
let make = (~items=[||], _children) => {
{
...component,
render: (_self) => {
}
}
};
When I compile this code I get this error.
# ERROR
We've found a bug for you!
/Users/jaisonjustus/code/todotabre/src/components/SortableContainer.re 5:34-48
3 │
4 │ let sortableContainerReactClass: ReasonReact.reactClass = sortableConta
iner(() => {
5 │ (ReasonReact.createElement(SortableElement))
6 │ });
7 │
The variant constructor SortableElement can't be found.
- If it's defined in another module or file, bring it into scope by:
- Annotating it with said module name: let food = MyModule.Apple
- Or specifying its type: let food: MyModule.fruit = Apple
- Constructors and modules are both capitalized. Did you want the latter?
Then instead of let foo = Bar, try module Foo = Bar.
What's wrong in this code?

aurelia/skeleton-plugin cant run test on custum element

i have created an aurelia plugin using the skelton-plugin https://github.com/aurelia/skeleton-plugin i am now looking at writing unit tests for it.
i am stuggling to get a unit test running for a custom element ive added to the project. i started with the 'testing a custom element' example from http://aurelia.io/hub.html#/doc/article/aurelia/testing/latest/testing-components/3
template:
<template>
<div class="firstName">${firstName}</div>
</template>
vm
import {bindable} from 'aurelia-framework';
export class MyComponent {
#bindable firstName;
}
i added this to the src folder.
my test code is
import {StageComponent} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrapper';
describe('MyComponent', () => {
let component;
beforeEach(() => {
component = StageComponent
.withResources('my-component')
.inView('<my-component first-name.bind="firstName"></my-component>')
.boundTo({ firstName: 'Bob' });
});
it('should render first name', done => {
component.create(bootstrap).then(() => {
const nameElement = document.querySelector('.firstName');
expect(nameElement.innerHTML).toBe('Bob');
done();
}).catch(e => { console.log(e.toString()) });
});
afterEach(() => {
component.dispose();
});
});
i jspm installed aurelia-bootstrapper and aurelia-testing to get it running.
im now getting the error
Error{stack: '(SystemJS) XHR error (404 Not Found) loading http://localhost:9876/base/my-component.js
so it looks like karma cant find my component. i checked the karma.config file and the jspm loadFiles: ['test/setup.js', 'test/unit/**/*.js'], looks correct.
has any one run into a similar issue?
solved the issue.
in karma.config.js file needed to change
serveFiles: ['src//.']
to
serveFiles: ['src//*.js', 'src/**/*.html']

Including and using Zend Service ReCaptcha in ZF2 (v2.3.3)

how to include Recaptcha service in zend framework 2?
I tried to do like this:
public function contactAction()
{
$formContact = new ContactForm();
$pubKey = 'mypubkey';
$privKey = 'myprivkey';
$recaptcha = new ZendService\ReCaptcha\ReCaptcha($pubKey, $privKey);
return array ('formContact' => $formContact, 'recaptcha' => $recaptcha);
}
but I discovered that ZendService\ReCaptcha is not present by default when you download the framework.
So, I downloaded it from here
https://github.com/zendframework/ZendService_ReCaptcha
and I placed it into vendor\zendframework\zendframework\library\zend together with the other parts of the library.
I tried to refresh the page but doesn't work again because it can't find the zend service recaptcha.
Fatal error: Class 'Application\Controller\ZendService\ReCaptcha\ReCaptcha' not found in C:\Program Files (x86)\xampp\htdocs\Zf-tutorial\module\Application\src\Application\Controller\IndexController.php on line 79
can someone help me? I thought it was simple to implement recaptcha, but it is not so ! thanks!
Add the zendservice-recaptcha module to your composer.json file and run an update:
{
...
"repositories": [
{
"type": "composer",
"url": "http://packages.zendframework.com/"
}
],
...
"require": {
...
"zendframework/zendservice-recaptcha": "*",
...
}
...
}
update composer :
php composer.phar update
This will install the module and configure the relevant class mapping and you will be able to access the classes by adding the use statements as with any other classes you use.
Even i tried recaptcha but with no success so implemented something different to refresh captcha and worked very well, try this once
resetCaptcha function:
$form = $this->getServiceLocator()->get('zfcuser_register_form');
$captcha = $form->get('captcha')->getCaptcha();
$data = array();
$data['id'] = $captcha->generate();
$data['src'] = $captcha->getImgUrl() .
$captcha->getId() .
$captcha->getSuffix();
return $data;
ajax request :
$(document).ready(function() {
$('#refreshcaptcha').click(function() {
var data = [];
var form = <?php $this->registerForm; ?>
data.push({name: "action", value: 'resetCaptcha'});
data.push({name: "params[form]", value: form});
$.post("<?php echo BASE_URL ?>/user/iajax", data,
function(data) {
$('#form_reg img').attr('src', data.src);
$('#captcha-id-hidden').attr('value', data.id);
}, 'json');
});
});
Html call :
<p class="refresh_captcha"><?php echo $this->formCaptcha($form->get('captcha')); ?>
<input type="button" id="refreshcaptcha" value="refresh">
</p>
You do not properly install the library ZendService\ReCaptcha.
your system write:
Class 'Application\Controller\ZendService\ReCaptcha\ReCaptcha' not found
You must:
placed it into vendor\zendframework\zendframework\library
In the file vendor/ZF2/library/Zend/Loader/StandardAutoloader.php insert string
$this->registerNamespace('ZendService', dirname(dirname((__DIR__)))
. '/ZendService');
in case self::AUTOREGISTER_ZF:
in the file init_autoloader.php insert string
$loader->add('ZendService', $zf2Path);.