React Native: Change boolean value of component property depending on if active or not

Asked
Active3 hr before
Viewed126 times

10 Answers

nativevaluepropertycomponentreact
90%

I have a React Native component that consists of 3 buttons components, each one has a boolean property called inner. If inner={true} then the button appears pressed in, whereas if inner={false} then the button appears embossed., What is the name of this kind of accommodation? , If someone helps stop a murder, what would be the possible consequences?

     <TouchableOpacity onPress={this.showAdd}>
        <Button inner={sceneType === 'add' }>Button 1</Button>
      <TouchableOpacity onPress={this.showRemove}>
        <Button inner={sceneType === 'remove'}>Button 2</Button>
      </TouchableOpacity>
      <TouchableOpacity onPress={this.showHistory}>
        <Button inner={sceneType=== 'history'}>Button 3</Button>
      </TouchableOpacity>
88%

Pretag
 Pretag team - issue, fix, solve, resolve
72%

Renders a boolean input.,This is a controlled component that requires an onValueChange callback that updates the value prop in order for the component to reflect user actions. If the value prop is not updated, the component will continue to render the supplied value prop instead of the expected result of any user actions.,The value of the switch. If true the switch will be turned on. Default value is false.

Pretag
 Pretag team - issue, fix, solve, resolve
65%

If you know the order and understand your cascades, you know that the secondary style “wins” in this example. But, the real answer is that there is no expected behavior and you should wrap the library within your code in such a way that prevents this from happening.,The reality is that this means that your Button component would better have been called MasterButtonSwitch since it has to juggle a lot depending on its arguments. In the case of these 3 booleans, there would be 8 distinct states to handle.,Another example where I’ve seen the “boolean trap” is when dealing with forms. Here’s one that we have in our codebase right now:

<button class="btn btn-primary">Primary</button>
<button class="btn btn-secondary">Secondary</button>
<button class="btn btn-danger">Danger</button>
load more v
75%

React is a wholly component-based architecture used to create a rich user interface and components.,Let’s say we have a component called Demo1, and we want to hide it based on the Boolean value true/false. We can use different conditional operators to achieve this functionality.,Props is a read-only piece of data which is used to consume some information or perform some action. Hence, props can also be used to hide or show a component.

1import React, { Component } from "react";
2
3class Demo1 extends Component {
4  constructor() {
5    super();
6    this.state = {
7      name: "React"
8    };
9  }
10
11  render() {
12    return <div>This is Demo1 component</div>;
13  }
14}
15
16export default Demo1;
load more v
40%

Takes a callback that is going to be triggered when state of the given handler changes.,In addition onHandlerStateChange event payload contains oldState attribute which represents the state of the handler right before the change.,Current state of the handler. Expressed as one of the constants exported under State object by the library. Refer to the section about handler state to learn more about how to use it.

Pretag
 Pretag team - issue, fix, solve, resolve
22%

success if the query has received a response with no errors and is ready to display its data. The corresponding data property on the query is the data received from the successful fetch or if the query's enabled property is set to false and has not been fetched yet data is the first initialData supplied to the query on initialization.,error if the query attempt resulted in an error. The corresponding error property has the error received from the attempted fetch,This function will fire any time the query is either successfully fetched or errors and be passed either the data or error

1
const {
   2 data, 3 dataUpdatedAt, 4 error, 5 errorUpdatedAt, 6 failureCount, 7 isError, 8 isFetched, 9 isFetchedAfterMount, 10 isFetching, 11 isIdle, 12 isLoading, 13 isLoadingError, 14 isPlaceholderData, 15 isPreviousData, 16 isRefetchError, 17 isStale, 18 isSuccess, 19 refetch, 20 remove, 21 status, 22
} = useQuery(queryKey, queryFn ? , {
   23 cacheTime,
   24 enabled,
   25 initialData,
   26 initialDataUpdatedAt27 isDataEqual,
   28 keepPreviousData,
   29 notifyOnChangeProps,
   30 notifyOnChangePropsExclusions,
   31 onError,
   32 onSettled,
   33 onSuccess,
   34 queryKeyHashFn,
   35 refetchInterval,
   36 refetchIntervalInBackground,
   37 refetchOnMount,
   38 refetchOnReconnect,
   39 refetchOnWindowFocus,
   40 retry,
   41 retryOnMount,
   42 retryDelay,
   43 select,
   44 staleTime,
   45 structuralSharing,
   46 suspense,
   47 useErrorBoundary,
   48
}) 49 50 // or using the object syntax51 52 const result = useQuery({53   queryKey,54   queryFn,55   enabled,56 })
60%

You should ask yourself whether this component owns this data. Were firstName and lastName created internally? If not, then the state should not depend on it.** And what is the best way to avoid this? Calculate fullName as a part of the render function.,If a component does not own a datum, then that datum should not influence it’s state.,Leave calculations and conditionals to the render function.

var React = require('react/addons');

class UserWidget extends React.Component {
  // ...

  // BAD: set this.state.fullName with values received through props
  constructor (props) {
    this.state = {
      fullName: `${props.firstName} ${props.lastName}`
    };
  }

  render () {
    var fullName = this.state.fullName;
    var picture = this.props.picture;

    return (
      <div>
        <img src={picture} />
        <h2>{fullName}</h2>
      </div>
    );
  }
}
load more v
48%

As you can see, we’ve added a tabIndex property, which we’re setting to 1 (focusable) or -1 (not focusable) depending on whether the component is currently disabled.,We’ll also need to import the correct file into our component:,Here’s a screenshot of the component we’ll be building:

Pretag
 Pretag team - issue, fix, solve, resolve
23%

Pretag
 Pretag team - issue, fix, solve, resolve

Other "native-value" queries related to "React Native: Change boolean value of component property depending on if active or not"