You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

linearGradientProps导致应用崩溃,该如何解决?

Fix for LinearGradientProps Causing App Crash

Hey there, let's get that gradient button working again! The error message is actually giving you the key to fix this—you just need to adjust how you pass the ViewComponent prop.

The Problem

Your current code is passing the LinearGradient component directly from the module:

<Button 
  text="SIGNUP" 
  ViewComponent={require('react-native-linear-gradient').LinearGradient} 
  linearGradientProps={{ colors: ['red', 'pink', 'red'], }} 
/>

But the Button component expects you to pass the entire module instead of the specific LinearGradient export. That's why the error says:

You need to pass a ViewComponent to use linearGradientProps ! Example: ViewComponent={require('react-native-linear-gradient')}

The Fix

Update your ViewComponent prop to pass the module directly, without adding .LinearGradient at the end:

<Button 
  text="SIGNUP" 
  ViewComponent={require('react-native-linear-gradient')} 
  linearGradientProps={{ colors: ['red', 'pink', 'red'], }} 
/>

Alternative (Cleaner ES6 Import)

If you prefer using ES6 imports (which is more readable), you can import the LinearGradient first and pass it directly:

import LinearGradient from 'react-native-linear-gradient';

// Then in your component:
<Button 
  text="SIGNUP" 
  ViewComponent={LinearGradient} 
  linearGradientProps={{ colors: ['red', 'pink', 'red'], }} 
/>

This works because when you import the default export from react-native-linear-gradient, you're essentially getting the same thing as the entire module reference from require.

Quick Check

Just make sure you've properly installed react-native-linear-gradient and linked it (if you're using a React Native version older than 0.60). Run these commands if you haven't already:

npm install react-native-linear-gradient
# For iOS (if needed)
cd ios && pod install && cd ..

内容的提问来源于stack exchange,提问作者Niklas Rosencrantz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:33:45