Is it possible to use two lodash set methods within a single function?

Is it possible to have two separate functions within one condition? My objective is to create 3 buttons that call one function to change the value of this.state.status based on the preferred condition.

'all' = null (this is my desired outcome, where the parameter default should be status=null or ' ') and I want this.state.status to be null

'approve' = approve (status=approve) my aim for this.state.status: approve

'progress' = progress (status=progress) my goal for this.state.status: progress


    this.state = {
      status: '',
    }
  }

//---------------------button sort--------------------------//
  onButtonChange = (status, value) => {
    setTimeout(() => AppActions.loadingMask(true))

    if (status === 'all')
    {
      _.set(this.state.data, status, null).set(this.state.status, status, ' ')
     // status = null - my goal is for this.state.status to be ' '
    }

    else if (status === 'approve')
    {
      _.set(this.state.data, status, 'approve').set(status, this.state.status, 'approve')
       // status = approve - my goal is for this.state.status to be approve
    }
    else if (status === 'progress')
    {
      _.set(this.state.data, status, 'progress').set(status, this.state.status, 'progress')
      // status = progress - my goal is for this.state.status to be progress
    }
    BookingsTripsAction.getTrips(
      this.DEFAULT_LIMIT,
      this.state.activePage,
      _.get(this.state.data, status, status))
  }


  <Col>
    <Paper>
      <FlatButton label="All" style={{margin: 12, borderBottom:(this.state.status == 'all' ? '1px solid #2196f3' : 0)}} onTouchTap={this.onButtonChange.bind(this, 'all')} />
      <FlatButton label="Approve" style={{margin: 12, borderBottom:(this.state.status == 'approve' ? '1px solid #2196f3' : 0)}} onTouchTap={this.onButtonChange.bind(this, 'approve')} />
      <FlatButton label="In-Progress" style={{margin: 12, borderBottom:(this.state.status == 'progress' ? '1px solid #2196f3' : 0)}} onTouchTap={this.onButtonChange.bind(this, 'progress')} />
    </Paper>
  </Col>

Answer №1

According to the guidance found in the reactjs documentation:

Avoid Directly Modifying State.

Modifying state directly will not trigger a re-render of your component. It is recommended to always utilize the setState() method for this purpose. For more details, refer to the information provided in the documentation about proper usage of state.

Answer №2

Implementing setState

if (status === 'all' || status === null)
    {
     this.setState({data:''});
    }

    else if (status ==='approve')
    {

     this.setState({data:status});
    }
    else if (status === 'progress')
    {
     this.setState({data:status});

    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instructions on how to eliminate the minutes button from Material UI datetime picker

I'm currently working on customizing a datetimepicker from materialUI in ReactJS. My goal is to prevent the user from seeing or selecting minutes in the picker interface. Despite setting the views prop to include only year, month, date, and hours, use ...

Using React and TypeScript together can lead to issues when trying to use union keys as an index

I've implemented a hook using useState and the delete method to effectively manage my form values. const [values, setValues] = useState<tAllValues>({}); The values stored include: { name: 'Andrew', age: 34, avatar: [{ name: ...

Mapping prop passed to client component in NEXT 13: A step-by-step guide

Hello, I'm currently navigating through the Next 13 APP directory and have encountered a scenario where everything functions smoothly when I integrate the server component as shown below: const Tasks = async () => { const { tasks } = await getAll ...

`Need help displaying table footer in XGrid material UI component?``

Can someone provide guidance on how to add a table footer to display column totals? I couldn't find any method to customize or append a custom table footer. import React from 'react'; import { XGrid, useGridApiRef } from '@material-ui/x ...

The component `style` requires a callback function as a getter for its configuration, but `undefined` was received instead. Remember to always capitalize component names at the beginning

When working with the MUI library in React Native, I encountered an issue: ERROR Invariant Violation: View config getter callback for component style must be a function (received undefined). Make sure to start component names with a capital letter. I ha ...

jss-rtl does not invert styles, meaning it does not reverse rules along the x-axis

I am currently working on a test application that utilizes Material UI with RTL enabled. I have set a margin left for my button in the application, but despite using jss-rtl, the margin does not switch to margin-right as expected - it remains as margin-lef ...

React.js is throwing a 429 error message indicating "Too Many Requests" when attempting to send 2 requests with axios

Currently, I am in the process of learning React with a project focused on creating a motorcycle specifications search web application. In my code file /api/index.js, I have implemented two axios requests and encountered an error stating '429 (Too Ma ...

What is the best way to center content in material-ui?

I have a login page featuring 2 text fields: one for username and one for password, as well as a login button. The username field is positioned at the top, the password field below it, and the login button at the bottom of the page. I would like all three ...

Looking to integrate WhatsApp into your next.js website for optimal responsiveness?

I am trying to incorporate WhatsApp into my next.js responsive website. I have already added an icon which successfully opens the WhatsApp web version when clicked in desktop view. However, I want to make sure that when a user accesses the website on mobi ...

Having trouble establishing a connection with mongoose and typescript

When attempting to establish a connection using mongoose, I consistently encounter the errors outlined below. However, if I use MongoClient instead, everything functions as expected. import connectMongo from '../../lib/connectMongo' console.log( ...

What are the steps for importing a file into a React app that is built using Create React App as plain text?

Objectives I aim to showcase code snippets from the project itself for reference. I intend to keep the displayed code up-to-date with its implementation. I prefer not to remove myself from create-react-app This React project, built using create-react-ap ...

MaterialUi SwipeableDrawer with a swipeableArea button

Trying to implement a swipeableDrawer feature with a button that dispatches an action upon click. However, struggling to click the button and swipe the drawer simultaneously. https://i.stack.imgur.com/6UZ52.png Desire to achieve both functionalities seam ...

Guide to Applying a Custom Style to Every Icon within the Array (React/Material-ui)

I'm currently working with material-ui icons and I have a scenario where I need to assign a specific icon for each object in an array. Instead of manually applying the "iconStyle" property to each individual icon in the array, I am looking for a more ...

What steps can be taken to ensure that the email input remains on the current page, eliminating the need for the user to re-enter their email address?

Initially, I implemented the reset password feature using AWS Amplify by creating two separate forms: one for forgotPassword, which takes usernames, and another for forgotPasswordSubmit to update the password. The setup involved multiple routes, and the pa ...

Steps to insert a new row for a grid item using material-ui

When it comes to breaking a line of grid item like this, the image shown below illustrates how the rest space of the grid should be empty. https://i.stack.imgur.com/pvSwo.png <Grid container spacing={3} <Grid item xs={12}> "Grid Item xs ...

MacOS web browsers adjust content to fit the screen size

As I delve into React development, one thing that has caught my attention is how MacOS browsers try to fit all the page content in view rather than allowing users to scroll. Let me illustrate this with an example: function Example() { const Elements = ...

What is the best way to create a React component that renders a class component as a functional component?

My Objective: At the moment, I am in the process of developing an AuthUserRole HOC component to manage user roles like Manager and Employee. However, I encountered a tutorial that uses a functional component to return a class component as referenced here. ...

Trouble with React Material Select Options Failing to Populate

After iterating and producing MenuItems, I am able to see the items when I console.log, but in the UI, the dropdown appears empty. I'm puzzled as to why the Select's are not being populated. Any thoughts on this issue? Below is the supplied code ...

Updating a nested object within an array in a ReactJs component

I am struggling with updating a nested object within an array in the state of my React application. My goal is to determine if an item already exists in the state based on its name. Here's what I'm aiming for: Add an item to the cart. If th ...

What is the process for setting up 127.0.0.1 with port 127.0.0.1:8000 in the .httaccess file?

Is there a way to set up a sub domain in Node.js while running on port 127.0.0.1:8000? Here is the .httaccess code: RewriteEngine On RewriteRule ^$ http://127.0.0.1:8080/ [P,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d Rewrit ...