score:2
Accepted answer
It took a bit to figure out but here is the solution.
- If you have not created a Reducer for Redux to store generic app settings create one. Here is my Reducer:
import {
Action,
Reducer
} from 'redux';
// -----------------
// STATE - This defines the type of data maintained in the Redux store.
export interface AppSettingsState {
ApiUrl: string
}
// -----------------
// ACTIONS - These are serializable (hence replayable) descriptions of state transitions.
// They do not themselves have any side-effects; they just describe something that is going to happen.
// Use @typeName and isActionType for type detection that works even after serialization/deserialization.
export interface SetApiUrlAction {
type: 'SET_APIURL';
apiUrl: string
}
// Declare a 'discriminated union' type. This guarantees that all references to 'type' properties contain one of the
// declared type strings (and not any other arbitrary string).
type KnownAction = SetApiUrlAction;
// ----------------
// ACTION CREATORS - These are functions exposed to UI components that will trigger a state transition.
// They don't directly mutate state, but they can have external side-effects (such as loading data).
export const actionCreators = {
setApiUrl: (url: string) => < SetApiUrlAction > {
type: 'SET_APIURL',
apiUrl: url
}
};
// ----------------
// REDUCER - For a given state and action, returns the new state. To support time travel, this must not mutate the old state.
export const reducer: Reducer < AppSettingsState > = (state: AppSettingsState, incomingAction: Action) => {
const action = incomingAction as KnownAction;
switch (action.type) {
case 'SET_APIURL':
return {
ApiUrl: action.apiUrl
};
default:
// The following line guarantees that every action in the KnownAction union has been covered by a case above
const exhaustiveCheck: never = action.type;
}
// For unrecognized actions (or in cases where actions have no effect), must return the existing state
// (or default initial state if none was supplied)
return state || {
ApiUrl: "http://localhost:5001/"
};
};
- After Creating the reducer and hooking it into the allReducers list you need to edit boot-server.tsx. After boot-server creates the state you can dispatch anything you need to. Here I grab the data from the asp-prerender-data tag I set in my MVC view.
store.dispatch({ type: 'SET_APIURL', apiUrl: params.data.apiUrl });
score:0
This other answer worked for me, I just had to add a const service class that would read this values How to push configuration values from Asp.Net Core MVC 2.0 config file to React TypeScript client script?
class ConfigService {
Uri: string;
public initialize(): void {
if (this.Uri) return;
var ell = document.getElementById('site-props');
var jsontext = ell!.innerText;
var siteProps = JSON.parse(jsontext);
this.Uri = siteProps.Uri;
}
}
const configService = new ConfigService();
export default configService;
Source: stackoverflow.com
Related Query
- How to pass AppSettings from ASP.NET Core MVC Controller to ReactJS / Redux ClientApp?
- How to post form data from reactjs to controller in mvc asp.net
- How to properly make REST calls from ReactJS + Redux application?
- moving from twig to ReactJS: how to extend a template/component to build full reactJS pages? implement react-router and redux into symfony?
- How to pass values from on component to Redux form
- Reactjs - How to pass values from child component to grand-parent component?
- Reactjs and redux - How to prevent excessive api calls from a live-search component?
- How To Pass Event From Redux To React?
- How to pass data from one component to another ReactJS
- How to pass data from one component to another while using API in reactjs
- How to push configuration values from Asp.Net Core MVC 2.0 config file to React TypeScript client script?
- How to Pass Key value from inputText change in ReactJs in typescript?
- How to pass data in server side rendering to reactjs component from node
- how to avoid key/id problems in reactjs and make props pass from parent to child?
- How do I pass redux store from one component to another?
- How to pass state from parent Component to child component in route (react-route-dom) reactjs
- Need Reactjs help on how to properly pass this function from the parent to child button component
- React Redux how to pass data from input to rest api
- How to pass props from container component to redux form(component) - react using typescript
- How to pass data from child component to parent component in reactjs
- ReactJS - Can't obtain parameters from url using Route. How can I pass params through url with Route?
- How can I make a function Async in reactjs which calls another two functions from redux actions
- How to pass props to reducer? ReactJs and Redux
- ReactJS + NextJS - How to pass props from _app.js to page?
- ReactJs Redux: how to remove duplicate objects (with duplicate values) from an array when deciding redux state (after map, filter, etc. functions)?
- How to pass argument from component to redux action
- How to pass state value from one class to another in Reactjs
- React hook form how to pass value from custom component to controller with already assign own on change?
- How to remove an element from an array in ReactJS Redux state, when a condition is true?
- ReactJS How to pass iterated values to child component from parent component using Context API
More Query from same tag
- How to share same function with multiple react components?
- Typescript: Type 'string' is not assignable to type
- How to make a react table whose rows and columns come from a JSON object
- Using refs in Three JS useFrame hook
- How to save details in MongoDb along with image
- Create a Comment Widget in React
- how to add inline style in a tag?
- How can I add an unchangeable property to a React JS component?
- How to add ripple effect when clicking Card in MUI
- How to track value of undefined variable in dependency list in useEffect in React and Next.js
- Get last 7 days' dates as an array from today's date using momentJs, ReactJs
- How to have a solid as well as dashed line in line chart highcharts
- Dealing with Nested React Components' State Changes
- ES6 Modules not importing as expected in React
- Adding data to state
- from class to functions React
- How to customize the calendar header text format (Material UI v5 - DatePicker)
- 'message' is not defined no-undef React Error
- " RuntimeError: Model class wagtail.core.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS"
- React Redux,Updation of particular item in state
- Module not found: Can't resolve 'bootstrap/dist/css/bootstrap.css' in 'C:\Users\test\counter-app\src'
- reactJS framework mouseOver and mouseEnter are not recognised, though onClick works fine
- React onClick taking an extra click to change state
- Cannot find module '@babel/preset-plugin-transform-object-assign'
- data can't be updated in mongodb
- How to change the style (background color of the summary row) of Sencha Extreact Grid product
- Fetch JSON values from another file in React
- Disabling style of other divs when cliking new one
- Validating file size and format with YUP
- React application with external plugins