react-state-flex

0.0.8 • Public • Published

React State Flex

React State Flex is a flexible state management library built on top of Redux Toolkit for React applications. It provides a convenient way to manage and persist application state with ease.

Installation

To install React State Flex, you can use npm or yarn:

npm install react-state-flex

or

yarn add react-state-flex

Getting Started

Using Flex Services

Utilize the useFlexService hook to interact with your state and perform API requests. This hook provides functions for sending data to your Flex store, detecting state changes, and making API calls with flexible configuration options.

FlexSender(stateName, value)

The FlexSender method dispatches an action to update the state in your Flex store. It takes two parameters:

  • stateName: The name of the state to update.
  • value: The new value to assign to the state.

Example usage:

FlexSender("count", 10);

This code dispatches an action to update the state named "count" to the value 10.

FlexDetector(stateName)

The FlexDetector method detects state changes by retrieving the value of a specific state slice from your Flex store. It takes one parameter:

  • stateName: The name of the state slice to retrieve.

Example usage:

const count = FlexDetector("count");

This code retrieves the current value of the state slice named "count" and assigns it to the variable count.

FlexApi Method

The FlexApi method allows you to make API requests with flexible configuration options. It integrates with the Axios library and provides additional features for managing state, progress tracking, exception handling, and status code handling.

Parameters:

  • config: An object containing the configuration options for the API request. This can include standard Axios configuration options such as method, url, data, etc., as well as the following additional options specific to React State Flex:
    • onUploadProgress: A function to be called periodically with progress information during the upload.
    • onDownloadProgress: A function to be called periodically with progress information during the download.
    • flexstore: An object containing options related to state management. This includes:
      • storeaccess: A boolean indicating whether to store the response data in the Flex store.
      • statename: (Optional) The name of the state slice to store the response data. If not provided, the response data will be stored in a default slice.

Returns:

  • A promise that resolves with the response data from the API request.

Example usage:

async function fetchData() {
  try {
    const response = await FlexApi({
      method: "GET",
      url: "https://api.example.com/data",
      onUploadProgress: (progress) => {
        console.log(`Upload Progress: ${progress}%`);
      },
      onDownloadProgress: (progress) => {
        console.log(`Download Progress: ${progress}%`);
      },
      flexstore: {
        storeaccess: true,
        statename: "myStateName", // Optional: Specify state to store response data
      },
    });
    console.log(response);
    // Handle successful response
  } catch (error) {
    console.error("Error:", error);
    // Handle error
  }
}

Added this FlexProvider in your index.js file

Example usage:

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <FlexProvider element={<App />} />
  </React.StrictMode>,
)

In this example, the FlexApi method is used to send a GET request to the specified URL. Progress tracking functions are provided for upload and download progress. Additionally, the response data is stored in the flex store under the state slice "myStateName". Error handling is included using a try-catch block to handle any errors that may occur during the API request.

Conclusion

React State Flex simplifies state management and provides a seamless way to handle state persistence, API requests, and error handling in your React applications. With its intuitive API and flexible configuration options, you can focus more on building your application logic without worrying about state management complexities.

Contact Information

If you have any questions or queries, please feel free to reach out to me via email at harshatalluru20@gmail.com.

Package Sidebar

Install

npm i react-state-flex

Weekly Downloads

5

Version

0.0.8

License

MIT

Unpacked Size

16.4 kB

Total Files

5

Last publish

Collaborators

  • harishtalluru