score:0
Try to create a function in place where your Popup
controller is, which dispatches something and pass it to that component. I mean something like:
const doSomething = () => {
dispatch(do());
};
const renderedContent = renderToString(
<Popup
port={port}
poiType={poiType}
activeView={activeView}
isPortsOnly={isPortsOnly}
locations={locations}
onDoSomething={doSomething}
/>
)
score:3
Well this was interesting! I've made a sandbox with the following code, scroll for some explanations:
Popup.js
import React, { useRef, useEffect } from "react";
import { connect } from "react-redux";
import { Popup } from "mapbox-gl";
import { zoomAction } from "./ActionCreators";
const CustomPopup = ({ zoomAction, lng, lat, zoom, map }) => {
const popUpRef = useRef(null);
useEffect(() => {
if (popUpRef.current && map) new Popup().setLngLat([lng, lat]).setDOMContent(popUpRef.current).addTo(map);
}, [popUpRef, map, lng, lat]);
const zoomIn = () => {
zoomAction();
map.zoomIn(zoom);
};
return (
<div ref={popUpRef}>
<h1>Hello World!</h1>
<button onClick={zoomIn}>zoom in (currently {zoom})</button>
</div>
);
};
const mapStateToProps = (state) => {
return { ...state };
};
const mapDispatchToProps = { zoomAction };
export default connect(mapStateToProps, mapDispatchToProps)(CustomPopup);
MapGl.js
import React, { useMemo, useState } from "react";
import MapboxGl from "mapbox-gl";
import { connect } from "react-redux";
import "mapbox-gl/dist/mapbox-gl.css";
import "./MapGl.css";
import Popup from "./Popup";
MapboxGl.accessToken = "pk.eyJ1IjoibmljZXk3MjMxMSIsImEiOiJja2hrdGE4MHkwMGxxMndteDRucGIyMDVqIn0.MKokCfCrE8VskLRUNGO0hw";
const MapGl = ({ zoom, lng, lat }) => {
const [isLoaded, setIsLoaded] = useState(false);
const map = useMemo(() => {
if (isLoaded)
return new MapboxGl.Map({
container: document.querySelector(".mapContainer"),
style: "mapbox://styles/mapbox/streets-v11",
center: [lng, lat],
zoom: zoom
});
}, [isLoaded]);
return (
<div>
<div ref={() => setIsLoaded(true)} className="mapContainer" />
{map && <Popup map={map} />}
</div>
);
};
const mapStateToProps = (state) => {
return { ...state };
};
export default connect(mapStateToProps)(MapGl);
MapGl.css
.mapContainer {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
}
App.js
import React from "react";
import "./App.css";
import MapGl from "./MapGl";
export default () => {
return <MapGl />;
};
Index.js
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import "./index.css";
import store from "./Store";
import App from "./App";
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
Store.js
import { createStore } from "redux";
const initialState = {
lng: -96,
lat: 37.8,
zoom: 3
};
function rootReducer(state = initialState, action) {
switch (action.type) {
case "zoom":
return { ...state, zoom: state.zoom + 1 };
default:
return state;
}
}
let store = createStore(rootReducer);
export default store;
ActionCreators.js
export const zoomAction = () => ({
type: "zoom"
});
First thing is to replace the setHtml
with setDOMContent
which takes an existing DOM node instead of a string. Secondly the use of renderToString
is tied to server-side rendering which is situational. In this example I make a popup with a button that dispatches a zoom action to update the store as well as syncing the map object itself. You can see the new zoom value applied to the map and the store value updated inside the popup.
Source: stackoverflow.com
Related Query
- Pass redux store to react-dom/server's renderToString() method
- Is it right way to pass Redux store via React Context in Root Component?
- React Redux - cannot use a pass down a component's method to the children - undefined error
- React DOM not updated when prop from redux store changes
- React dom not updating with Redux store
- Redux failing to to pass store to React component
- React js onClick can't pass value to method
- What is the best way to access redux store outside a react component?
- When do I choose React state Vs Redux Store
- Accessing Redux Store from routes set up via React Router
- React get state from Redux store within useEffect
- React Redux - Is adding async method in a Reducer an anti pattern?
- React Redux Store updating, but component not re-rendering
- Jest/Enzyme Unit Testing: How to pass store to shallow component that uses redux 4 and react-redux 6 connect function
- What is the necessity of "export default connect" when you are connecting your react component to redux store
- Test React component method is calling function pass as a prop
- How do I use local state along with redux store state in the same react component?
- React Redux unexpected key passed to create store
- Where to store WebRTC streams when building React app with redux
- Access Redux store outside React when using next-redux-wrapper
- React Native: HeadslessJS and Redux - How to access store from task
- Can not pass state with react router dom v6 beta, state is null
- Pass React Ref from parent to child in order to get DOM element
- Pass data between 2 React DOM
- Pass DOM Element as a prop in React
- Developing a Redux store as a seperate NPM module for a React App
- React Redux pass state as props to component
- react props not updating with redux store
- Best way to store file objects in React Redux store (files upload from DropZone?)
- React Redux - Error passing several store enhancers to createStore()
More Query from same tag
- How to wait for getDownloadURL to finish in my mapping function before updating my object array in react state?
- How to apply Formik with Chakra in typescript?
- How can I handle redux form submission in child component?
- Implement Array on Form Field Using Index and Spread Operator
- How can I calculate the differences between now date and constant date. And edit it like 1d,1m,1w
- Render table cells (td) with different HTML tags in React
- Resetting input field (controlled component) in a nested state in React
- Render array data in table inside return of a function - Reactjs Javascript
- iterate through undefined/null objects in a react element
- Error: Too many re-renders. because i changed setState
- On minimization images go down from position CSS
- How to nest FieldArray functionality inside other <FieldArray> component with React-final-form
- next-with-apollo, SSR does not work, request is done on client
- How to programmatically reset scroll in react-table
- how to type react-redux connect in flow?
- How do I call back to a class' state values in React?
- getServerSideProps access current browser url
- React Redux router saga material-ui and tabs corresponding to current route
- MERN: How to access build folder for Heroku deployment?
- extra (unwanted) space between grids
- Redirect to other page after logging in or out without loosing state
- React Hooks Problem: Unhandled Rejection (TypeError): Cannot read property 'signup' of undefined
- Why does this log [object Object], [object Object]?
- Typescript and Context API, Error: Rendered more hooks than during the previous render
- React Dynamically using Component Names
- Communication between two React form components with Redux
- Filter list of Objects according to the month?
- react-select detached reset button does not work with disabled attribute
- Unable to send POST request from the React frontend to Django Rest Framework using serializer?
- CKEditor: Image uploaded but still getting an alert saying can't upload file:filename (using ckfinder) in react