I am trying to use leaflet-geoman library in a React project. I need to create a custom toolbar button that enables and disables global drag mode.
When toolbar button is clicked, map.pm.enableGlobalDragMode(); function enables global mode. When toolbar button is clicked again, map.pm.disableGlobalDragMode(); causes useCallback function afterClick running in infinite loop.
codesandbox.io
useDraw.js
import React from "react";
const useDraw = (mapRef) => {
const afterClick = React.useCallback(() => {
console.log("afterclick");
const map = mapRef.current.leafletElement;
let editDragEnabled = false;
if (!editDragEnabled) {
console.log("enable");
map.pm.enableGlobalDragMode();
editDragEnabled = true;
} else {
console.log("disable");
map.pm.disableGlobalDragMode();
editDragEnabled = false;
}
}, [mapRef]);
React.useEffect(() => {
const map = mapRef.current.leafletElement;
var actions = ["finishMode"];
map.pm.addControls({
drawRectangle: false,
drawMarker: false,
drawPolyline: false,
drawPolygon: false,
drawCircle: false,
drawCircleMarker: false,
removalMode: false,
editMode: false,
cutPolygon: false,
dragMode: false
});
map.pm.Toolbar.createCustomControl({
name: "DragEdit",
block: "custom",
title: "Edit and Drag Layers",
onClick: () => afterClick(),
actions: actions,
toggle: true
});
}, [mapRef, afterClick]);
};
export default useDraw;
The problem is, that when enableGlobalDragMode ( or disable) the Control of the original drag button is activated and this disables your custom button (because all other buttons are disabled, so that only one mode can be active).
I suggest to use the code from the enableGlobalDragMode function instead of calling it, which cause a change on the controls:
const afterClick = React.useCallback(() => {
console.log("afterclick");
const map = mapRef.current.leafletElement;
const layers = L.PM.Utils.findLayers(map);
let dragMode = map.pm._customModeEnabled || false;
if(!dragMode){
console.log("enable");
layers.forEach((layer)=>{
layer.pm.enableLayerDrag();
})
}else{
console.log("disable");
layers.forEach((layer)=>{
layer.pm.disableLayerDrag();
})
}
map.pm._customModeEnabled = !dragMode;
}, [mapRef]);
Related
I have a set of radio button. I have managed state by useState. when the user select one radio button, the other radio buttons should turn false. I am trying a basic if else to deal with this. I feel I am doing it wrong and can be done more efficiently. Is there any other way to do it. Kindly help.
const [trans,setTrans] = useState({
expense : false, income : false,
lend : false}
);
const setRadio = (event)=>{
const buttonName = event.target.id;
if(buttonName==="expense"){
setTrans((prev) =>({...prev,[buttonName]:true,income:false,lent:false}));
}
if(buttonName==="income"){
setTrans((prev) =>({...prev,[buttonName]:true,expense:false,lent:false}));
}
if(buttonName==="lent"){
setTrans((prev) =>({...prev,[buttonName]:true,income:false,expense:false}));
}
}
No need to populate the state. You can do something like this:
// initialState should be outside your component
let initialState = {
expense: false,
income: false,
lend: false,
};
const [trans, setTrans] = useState({
...initialState,
});
const setRadio = (event) => {
const buttonName = event.target.id;
let changedTrans = {
...initialState,
[buttonName]: true,
};
setTrans(changedTrans);
};
Codesandbox link for reference: https://codesandbox.io/s/radio-react-jlxhuf
I learn React and now I use the Uppy so user can select files for upload.
When user have select his file the files are hidden by settting showSelectedFiles={false}
I use my own Component to show the selected files and I get the files using this:
.on("file-added", (file) => {
const { setFile } = props;
setFile(file);
const newList = this.state.files.concat({ file });
this.setState({
files: { newList },
});
});
For each file added to the Dashboard the setFile(file); is sending the file object to my Custom view. The problem is that the preview image Blob that is auto created by the Dashboard is not present at this stage.
How can I get the files to my Custom GUI to show them including the image preview Blob?
I'm new to React and JavaScript so please be gentle:)
Complete code:
import React from "react";
import "#uppy/status-bar/dist/style.css";
import "#uppy/drag-drop/dist/style.css";
import "#uppy/progress-bar/dist/style.css";
import "./styles.css";
import "#uppy/core/dist/style.css";
import "#uppy/dashboard/dist/style.css";
const Uppy = require("#uppy/core");
// const Dashboard = require("#uppy/dashboard");
const GoogleDrive = require("#uppy/google-drive");
const Dropbox = require("#uppy/dropbox");
const Instagram = require("#uppy/instagram");
const Webcam = require("#uppy/webcam");
const Tus = require("#uppy/tus");
const ThumbnailGenerator = require("#uppy/thumbnail-generator");
const {
Dashboard,
DashboardModal,
DragDrop,
ProgressBar,
} = require("#uppy/react");
class DashboardUppy extends React.Component {
constructor(props) {
super(props);
this.form = React.createRef();
this.state = {
showInlineDashboard: false,
open: false,
files: [],
};
this.uppy = new Uppy({
id: "uppy1",
autoProceed: false,
debug: true,
allowMultipleUploads: true,
proudlyDisplayPoweredByUppy: true,
restrictions: {
// maxFileSize: 1000000,
maxNumberOfFiles: 100,
minNumberOfFiles: 1,
allowedFileTypes: null,
},
onBeforeFileAdded: (currentFile, files) => {
console.log(files);
const modifiedFile = Object.assign({}, currentFile, {
name: currentFile + Date.now(),
});
if (!currentFile.type) {
// log to console
this.uppy.log(`Skipping file because it has no type`);
// show error message to the user
this.uppy.info(`Skipping file because it has no type`, "error", 500);
return false;
}
return modifiedFile;
},
})
.use(Tus, { endpoint: "https://master.tus.io/files/" })
.use(GoogleDrive, { companionUrl: "https://companion.uppy.io" })
.use(Dropbox, {
companionUrl: "https://companion.uppy.io",
})
.use(Instagram, {
companionUrl: "https://companion.uppy.io",
})
.use(Webcam, {
onBeforeSnapshot: () => Promise.resolve(),
countdown: false,
modes: ["video-audio", "video-only", "audio-only", "picture"],
mirror: true,
facingMode: "user",
locale: {
strings: {
// Shown before a picture is taken when the `countdown` option is set.
smile: "Smile!",
// Used as the label for the button that takes a picture.
// This is not visibly rendered but is picked up by screen readers.
takePicture: "Take a picture",
// Used as the label for the button that starts a video recording.
// This is not visibly rendered but is picked up by screen readers.
startRecording: "Begin video recording",
// Used as the label for the button that stops a video recording.
// This is not visibly rendered but is picked up by screen readers.
stopRecording: "Stop video recording",
// Title on the “allow access” screen
allowAccessTitle: "Please allow access to your camera",
// Description on the “allow access” screen
allowAccessDescription:
"In order to take pictures or record video with your camera, please allow camera access for this site.",
},
},
}).use(ThumbnailGenerator, {
thumbnailWidth: 200,
// thumbnailHeight: 200 // optional, use either width or height,
waitForThumbnailsBeforeUpload: true
})
.on("thumbnail:generated", (file, preview) => {
const img = document.createElement("img");
img.src = preview;
img.width = 100;
document.body.appendChild(img);
})
.on("file-added", (file) => {
const { setFile } = props;
setFile(file);
const newList = this.state.files.concat({ file });
this.setState({
files: { newList },
});
});
}
componentWillUnmount() {
this.uppy.close();
}
render() {
const { files } = this.state;
this.uppy.on("complete", (result) => {
console.log(
"Upload complete! We’ve uploaded these files:",
result.successful
);
});
return (
<div>
<div>
<Dashboard
uppy={this.uppy}
plugins={["GoogleDrive", "Webcam", "Dropbox", "Instagram"]}
metaFields={[
{ id: "name", name: "Name", placeholder: "File name" },
]}
open={this.state.open}
target={document.body}
onRequestClose={() => this.setState({ open: false })}
showSelectedFiles={false}
/>
</div>
</div>
);
}
}
export default DashboardUppy;
Ran into this problem as well because I wanted to use the image preview to figure out the aspect ratio of the underlying image.
If you're using Dashboard or ThumbnailGenerator for Uppy, an event is emitted for every upload:
uppy.on('thumbnail:generated', (file, preview) => {
const img = new Image();
img.src = preview;
img.onload = () => {
const aspect_ratio = img.width / img.height;
// Remove image if the aspect ratio is too weird.
// TODO: notify user.
if (aspect_ratio > 1.8) {
uppy.removeFile(file.id);
}
}
});
I realize though that you already are looking for this event in your code. I guess to answer your question, just put your logic there instead of in file-added.
I am using Highcharts React wrapper in an app using Hooks, when my chart is either loaded or zoomed it fires both setExtremes and setAfterExtremes multiple times each. I've looked through for similar questions but they are related to different issues.
I've reduced the code to the minimum setup, the page is not refreshing, the data is only parsed once and added to the chart once yet, animation is disabled and it's still consistently firing both events 7 times on:
* initial population
* on zoom
Versions: react 16.9, highcharts 7.2, highcharts-react-official 2.2.2
Chart
<HighchartsReact
ref={chart1}
allowChartUpdate
highcharts={Highcharts}
options={OPTIONS1}
/>
Chart Options:
const [series1, setSeries1] = useState([]);
const OPTIONS1 = {
chart: {
type: 'spline',
zoomType: 'x',
animation: false
},
title: {
text: ''
},
xAxis: {
events: {
setExtremes: () => {
console.log('EVENT setExtremes');
},
afterSetExtremes: () => {
console.log('EVENT sfterSetExtremes');
}
},
},
plotOptions: {
series: {
animation: false
}
},
series: series1
};
Data Population:
useEffect(() => {
if (data1) {
const a = [];
_.each(data1.labels, (sLabel) => {
a.push({
name: sLabel,
data: [],
})
});
... POPULATES DATA ARRAYS...
setSeries1(a);
}
}, [data1]);
Rather the question is old I also faced the same situation. The solution is to move the chart options to a state variable. Then the event will not fire multiple times.
It is mentioned on the library docs. https://github.com/highcharts/highcharts-react -- see the "optimal way to update"
import { render } from 'react-dom';
import HighchartsReact from 'highcharts-react-official';
import Highcharts from 'highcharts';
const LineChart = () => {
const [hoverData, setHoverData] = useState(null);
// options in the state
const [chartOptions, setChartOptions] = useState({
xAxis: {
categories: ['A', 'B', 'C'],
events: {
afterSetExtremes: afterSetExtremes,
},
},
series: [
{ data: [1, 2, 3] }
],
});
function afterSetExtremes(e: Highcharts.AxisSetExtremesEventObject) {
handleDateRangeChange(e);
}
return (
<div>
<HighchartsReact
highcharts={Highcharts}
options={chartOptions}
/>
</div>
)
}
render(<LineChart />, document.getElementById('root'));```
Your useEffect is getting fired multiple times probably because you are checking for data1 and data1 is changing. have you tried putting an empty array in your useEffect and see if it is firing multiple times?
if it only fires up once then the problem is that your useEffect is checking for a value that is constantly changing
if it still fires multiple times then there is something that is triggering your useEffect
I struggled the same problem after I SetState in useEffect().
My problem was I did a (lodash) deepcopy of the Whole options.
This also create a new Event every time.
// Create options with afterSetExtremes() event
const optionsStart: Highcharts.Options = {
...
xAxis: {
events: {
afterSetExtremes: afterSetExtremesFunc,
}
},
....
// Save in state
const [chartOptions, setChartOptions] = useState(optionsStart);
// On Prop Change I update Series
// This update deepcopy whole Options. This adds one Event Every time
React.useEffect(() => {
var optionsDeepCopy = _.cloneDeep(chartOptions);
optionsDeepCopy.series?.push({
// ... Add series data
});
setChartOptions(optionsDeepCopy);
}, [xxx]);
The fix is to Only update the Series. Not whole Options.
React.useEffect(() => {
var optionsDeepCopy = _.cloneDeep(chartOptions);
optionsDeepCopy.series?.push({
// ... Add series data
});
const optionsSeries: Highcharts.Options = { series: []};
optionsSeries.series = optionsDeepCopy.series;
setChartOptions(optionsSeries);
}, [xxx]);
I am trying to implement an "add all" button in my react app. to do that, i pass this function to the onClick method of the button :
for (element in elements) {
await uploadfunction(element)
}
const uploadfunction = async (element) => {
if (valid) {
// await performUpload(element)
}
else if (duplicate) {
//show dialog to confirm upload - if confirmed await performUpload(element)
}
else {
// element not valid set state and show failed notification
}
}
const performUpload = async (element) => {
// actual upload
if(successful){
// set state
}else{
// element not successful set state and show failed notification
}
}
the uploadfunction can have three different behaviors :
Add the element to the database and update the state
Fail to add the element and update the state
Prompt the user with the React Dialog component to ask for confirmation to add duplicat element and update the state accordingly
My problem now is since i'm using a for loop and despite using Async/await , i can't seem to wait for user interaction in case of the confirmation.
The behavior i currently have :
The for loop move to the next element no matter what the result
The Dialog will show only for a second and disappear and doesn't wait for user interaction
Wanted behavior:
Wait for user interaction (discard/confirm) the Dialog to perform the next action in the loop.
How can i achieve that with React without Redux ?
Here is an example of a component that might work as an inspiration for you.
You might split it in different components.
class MyComponent extends Component {
state = {
items: [{
// set default values for all booleans. They will be updated when the upload button is clicked
isValid: true,
isDuplicate: false,
shouldUploadDuplicate: false,
data: 'element_1',
}, {
isValid: true,
isDuplicate: false,
shouldUploadDuplicate: false,
data: 'element_1',
}, {
isValid: true,
isDuplicate: false,
shouldUploadDuplicate: false,
data: 'element_2',
}],
performUpload: false,
};
onUploadButtonClick = () => {
this.setState(prevState => ({
...prevState,
items: prevState.items.map((item, index) => ({
isValid: validationFunction(),
isDuplicate: prevState.items.slice(0, index).some(i => i.data === item.data),
shouldUploadDuplicate: false,
data: item.data
})),
performUpload: true,
}), (nextState) => {
this.uploadToApi(nextState.items);
});
};
getPromptElement = () => {
const firstDuplicateItemToPrompt = this.getFirstDuplicateItemToPrompt();
const firstDuplicateItemIndexToPrompt = this.getFirstDuplicateItemIndexToPrompt();
return firstDuplicateItemToPrompt ? (
<MyPrompt
item={item}
index={firstDuplicateItemIndexToPrompt}
onAnswerSelect={this.onPromptAnswered}
/>
) : null;
};
getFirstDuplicateItemToPrompt = this.state.performUpload
&& !!this.state.items
.find(i => i.isDuplicate && !i.shouldUploadDuplicate);
getFirstDuplicateItemIndexToPrompt = this.state.performUpload
&& !!this.state.items
.findIndex(i => i.isDuplicate && !i.shouldUploadDuplicate);
onPromptAnswered = (accepted, item, index) => {
this.setState(prevState => ({
...prevState,
items: prevState.items
.map((i, key) => (index === key ? ({
...item,
shouldUploadDuplicate: accepted,
}) : item)),
performUpload: accepted, // if at last an item was rejected, then the upload won't be executed
}));
};
uploadToApi = (items) => {
if (!this.getFirstDuplicateItemToPrompt()) {
const itemsToUpload = items.filter(i => i.isValid);
uploadDataToApi(itemsToUpload);
}
};
render() {
const { items } = this.stat;
const itemElements = items.map((item, key) => (
<MyItem key={key} {...item} />
));
const promptElement = this.getPromptElement();
return (
<div>
<div style={{ display: 'flex', flexDirection: 'row' }}>
{itemElements}
</div>
<Button onClick={this.onUploadButtonClick}>Upload</Button>
{promptElement}
</div>
)
}
}
I am creating a google maps react application where i can create, edit and remove objects from then map.
When i press a marker the google map click listener will display its data in a form where i can edit it. If i do start editing the data in my form i set the "isEditingMapObject" state too true. This is done with useContext.
Now my goal is to prevent other click listeners to run their code when "isEditingMapObject" state is true. I figured this would be easy with a if check. But it seems the click listeners "isEditingMapObject" state never changes. It will always stay false.
const Markers = ({ googleMap, map }) => {
const mapContext = useContext(MapContext);
const mapMarkerReducer = (state, action) => {
switch (action.type) {
case "ADD":
return state.concat(action.payload.marker);
case "UPDATE":
return state.filter(marker => marker._id !== action.payload._id).concat(action.payload.marker);
case "SET":
return action.payload;
case "REMOVE":
return state.filter(marker => marker._id !== action.payload._id);
default:
return state;
}
}
const [markers, markersDispatch] = useReducer(mapMarkerReducer, []);
useEffect(() => {
if (!googleMap || !map) {
return;
}
axios
.get(constants.API_MARKERS)
.then(result => {
console.log("success");
const markersLoaded = [];
for (const marker of result.data) {
const iconPath = marker.type == "info" ? iconImportant : iconMailbox;
const markerObj = new googleMap.maps.Marker({
zIndex: 115,
position: { lat: marker.latitude, lng: marker.longitude },
icon: {
url: iconPath,
scaledSize: new googleMap.maps.Size(15, 15)
},
type: "marker",
marker: marker,
map: map,
visible: mapContext.markersIsVisible
});
googleMap.maps.event.addListener(markerObj, "click", (marker) => {
if(mapContext.isEditingMapObject) {
return;
}
mapContext.setActiveMapObjectHandler(marker);
});
markersLoaded.push(markerObj)
}
markersDispatch({ type: "SET", payload: markersLoaded });
})
.catch(error => {
console.log(error.response);
});
}, [googleMap, map]);
return null
};
export default Markers;
I expected the if statement to trigger return when state of mapContext.isEditingMapObject is true. But it is always false inside the listener. When logging state outside of the listener, mapContext.isEditingMapObject is showing correct value.
The map listener is storing the state when declared. And is not referencing to the same object as react state is. Therefore any changes in react state will have no effect on the listener.
workaround solution was to update react state when lister is triggered. And then use useEffect to trigger on that state change. In there i then access the current react state. Its not pretty! I have found no other working solution:
const [clickedMapObjectEvent, setClickedMapObjectEvent] = useState(null);
googleMap.maps.event.addListener(markerObj, "click", (marker) => {
//Can not access react state in here
setClickedMapObjectEvent(marker);
});
useEffect(() => {
//Access state in here
}, [clickedMapObjectEvent])