Im using material-ui and react-router-dom in my react (typescript) application
I have tried to use the following tip/help, so that when a user clicks a button (or whatever) he gets redirected to that route. Very basic stuff. When I click on the button, the url is updated in the url-window in my browser, but the correct component is not loaded. I had this working using bootstrap (and 'react-router-bootstrap's NavigationLink), but now Im stuck. Any tips how to get this working
(Im also using Mobx as state handler), Im also happy with any other router, if that makes my life simpler
https://material-ui.com/demos/buttons/
and
How to make a Material UI react Button act as a react-router-dom Link?
Note that if I hit enter in the url-window I get routed correctly (to right component)
I'm using this method to achieve a correct react-router-dom link for a Button component from material-ui.
Define a LinkComponent
import * as React from 'react';
import { Link } from 'react-router-dom';
export const LinkComponent = (props: any) => {
return <Link {...props} />;
};
Then you can use it like this:
<Button
variant={'outlined'}
color={'primary'}
component={LinkComponent}
to={'/'}>
HomePage
</Button>
If you don't want to create a dedicated component for this purpose, you can use it as a lambda function as a component prop for the <Button>.
Related
I have minimized the page to only mateiral ui component and its rendering page but the problem still exists. when I refresh the page, every once in a while a big star flashes before the page is rendered. i suspect it is un-styled star of the mateiral-ui rating component. as I add more imports and complexity to the page, the rate of the star appearing increases. I was wondering if what i'm doing is wrong
import Rating from "#material-ui/lab/Rating";
function Event() {
return (<Rating
size="large"
readOnly={true}
defaultValue={5}
precision={0.5}
/>
);
}
export async function getServerSideProps(context) {
return {props:{}}
}
export default Event
using nextjs version 9.3
express code:
app.get('/event/:title/:id', async (request, response) => {
const data = {}
return next.render(request, response, '/event/Event', data);
});
It's important to provide the page with the required CSS, otherwise, the page will render with just the HTML then wait for the CSS to be injected by the client, causing it to flicker (FOUC). To inject the style down to the client:
Create a fresh, new ServerStyleSheets instance on every request.
Render the React tree with the server-side collector.
Pull the CSS out.
Pass the CSS along to the client.
On the client-side, the CSS will be injected a second time before removing the server-side injected CSS. You can see how to do so in this example.
More about Server Rendering with Material UI
I am creating very basic ionic app. I want to show splash, then admob interstitial and on close of interstitial, i want to redirect to home page.
The only problem which I am facing here is updating the view in the home page. In home page, i have very simple text box and button. I am using 2 way data binding here and its not working at all.
I have created repo for this if somebody wants to have a look and let me know why the view is not updating.
https://github.com/krishnaa99/admobissue
Demo Video
https://www.youtube.com/watch?v=t_BKJ1mGpag
if the value is changing in component but not in view(html) then after action in component use this.
import { ChangeDetectorRef } from '#angular/core';
constructor(private changeRef: ChangeDetectorRef){}
this.changeRef.detectChanges();
i hope it can help.
Possible solutions you can try
you should check the logs for errors
Assign clear type to "input" variable. ( try making it explicitly
public )
"input" may be considered as a keyword, try using a different
variable name ( less likely )
Using Material-UI v1,
I want to access root inside styles of the Button component, and to use it for a whole other component (an input).
Doing import { styles } from 'material-ui/Button'; doesn't seem to work.
Is it possible? if so, then how?
Was able to achieve this using simply import { styles } from 'material-ui/Button/Button';
Currently I do this by adding a Javascript event handler to the HTML content which sets a global variable according to the clicked item:
<script>
var clicked="";
</script>
<span onmousedown="clicked='me';">me</span>
then from the main application I query the content of this variable in the webview's mouse clicked handler:
webview.setOnMouseClicked(new EventHandler[MouseEvent]{
def handle(mouseEvent:MouseEvent){
if(mouseEvent.getEventType().toString()=="MOUSE_CLICKED"){
val clicked=webview.executeScript("clicked").toString()
}}})
This happens to work but it feels like a hack. Is there a legitimate way by which the webview can request the application to refresh its content based on the element that was clicked?
As I do not know Scala, I can only provide you with an approach in Java, but you can probably figure out, how to transform it to Scala.
Register a bridge between the Java (or in your case Scala) code and JavaScript. The basics are described in this article by Oracle. The JavaScript in your page can make a call to the application, notifying it that a certain button was clicked:
public class MyBridge {
public void callbackFromJavaScript(String what) {
...
}
}
JSObject jsobj = (JSObject) webEngine.executeScript("window");
jsobj.setMember("java", new MyBridge());
Then you can make the call when an element is clicked and callbackFromJavaScript will be executed:
<span onmousedown="java.callbackFromJavaScript('me');">me</span>
From your callbackFromJavaScript you can then easily call a JavaScript function and pass along an object (e.g. JSON) and the JS function will update the page, or you can load a completely different content.
As we are facing GWT performance issues in a mobile app I peeked into Google Wave code since it is developed with GWT.
I thought that all the buttons there are widgets but if you look into generated HTML with firebug you see no onclick attribute set on clickable divs. I wonder how they achieve it having an element that issues click or mousedown events and seemingly neither being a widget nor injected with onclick attribute.
Being able to create such components would surely take me one step further to optimizing performance.
Thanks.
ps: wasnt google going to open source client code too. Have not been able to find it.
You don't have to put an onclick attribute on the HTML to make it have an onclick handler. This is a very simple example:
<div id="mydiv">Regular old div</div>
Then in script:
document.getElementById('mydiv').onclick = function() {
alert('hello!');
}
They wouldn't set the onclick property directly, it would have been set in the GWT code or via another Javascript library.
The GWT documentation shows how to create handlers within a GWT Java app:
public void anonClickHandlerExample() {
Button b = new Button("Click Me");
b.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// handle the click event
}
});
}
This will generate an HTML element and bind a click handler to it. However, in practice this has the same result as using document.getElementById('element').onclick() on an existing element in your page.
You can hook functions to the onclick event using JavaScript. Here's an example using jQuery:
$(document).ready(function(){
$("#div-id").click(function(){
/* Do something */
});
});
If you're interested in optimizing performance around this, you may need to investigate event delegation, depending on your situation.
A click event is generated for every DOM element within the Body. The event travels from the Body down to the element clicked (unless you are using Internet Explorer), hits the element clicked, and then bubbles back up. The event can be captured either through DOM element attributes, event handlers in the javascript, or attributes at any of the parent levels (the bubbling or capturing event triggers this).
I'd imagine they've just set it in a .js file.
Easily done with say jQuery with $(document).ready() for example.