S
S
Shiki002020-08-18 15:14:37
React
Shiki00, 2020-08-18 15:14:37

Redux does not add a property to state. What to do?

I want to add a basket: "true" property to every product that I click on.
Action.js:

export const addToBasket = (name) => {
     return {
       type: "BASKET",
       payload: name,
     };
    }

reducer:
import Alert from 'react-native'
export default function (state=null, action) {
  switch (action.type) {
    case "BASKET":
       var productToUpdate = state.products.find(item => item.name === action.payload);
       productToUpdate = { ...productToUpdate, basket: true }
       var products = state.products.filter(item => item.name !== productToUpdate.name);
       Alert.alert(productToUpdate)
       return { ...state, ...state.products, productToUpdate }
  
      break;
    default:
      return state;
  };
}

ProductDetails. Here I am using the reducer in
<Text onPress={() => this.props.addToBasket.bind(this, this.props.product.name)} style={styles.name}>{this.props.product.name}</Text>:

Here is the complete ProductDetails code (excluding styles and imports):
ProductDetails extends Component {
    render() {
       const { navigation } = this.props;
      if (!this.props.product) {
        return (
          <View>
            <Text>Choose a product</Text>
          </View>
        );
      }
       return (
    
         <View style={styles.productDetailsContainer}>
           <Image
              style={styles.productImg}
              source={{
                uri: this.props.product.img,
              }}
            />
            <View styles={styles.row}>
              <View>
                <Text onPress={() => this.props.addToBasket.bind(this, this.props.product.name)} style={styles.name}>{this.props.product.name}</Text>
                <Text style={styles.price}>{this.props.product.price}</Text>
              </View>
              <Text style={styles.flexEnd} onPress={() => navigation.navigate('Basket')}>Привет!</Text>
            </View>
          </View>
       );
     }
    }
    function mapStateToProps (state) {
      return {
        product: state.active
      };
    }
    function matchDispatchToProps (dispatch) {
      return bindActionCreators({addToBasket: addToBasket}, dispatch)
    }
    export default connect (mapStateToProps, matchDispatchToProps)(ProductDetails);

Basket. Here I am displaying the products with basket: "true" using the showProductsList() function
class Basket extends Component {
    render() {
       const showProductsList = (type, maxlength) => {
         var currentLength = 0;
         return this.props.products.map ((product) => {
           if(product.basket == type & currentLength < maxlength) {
             currentLength += 1;
             return (
                    <View key={product.id}>
                 <Image
                   source={{
                     uri: product.img,
                   }}
                 />
                   <View>
                     <Text>{product.name}</Text>
                     <Text>Price: {product.price}</Text>
                   </View>
                 </View>
             );
           }
         })
    
       }
       if (!this.props.products) {
         return (
           <View>
             <Text>Выберите товар</Text>
           </View>
         );
       } else {
       return (
         <View>
          {showProductsList("true", 4)}
         </View>
       )
    }
    }
    }
    
    function mapStateToProps(state) {
      return {
        products: state.products
      };
    }
    
    export default connect(mapStateToProps)(Basket);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-08-18
@0xD34F

case 'BASKET':
  return {
    ...state,
    products: state.products.map(n => n.name === action.payload
      ? { ...n, basket: true }
      : n
    ),
  };

UPD. Well, this is already at any gate:
onPress={() => this.props.addToBasket.bind(this, this.props.product.name)}

That's why, why are you pulling your little hands in the direction of all kinds of react'ov, not knowing the language? No context binding needed here:
onPress={() => this.props.addToBasket(this.props.product.name)}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question