首页 > 解决方案 > 如何通过组件反应原生的样式传递道具?

问题描述

我正在尝试在本机反应中制作一个深色主题按钮主题,因此我需要通过主题提供程序通过 BackgroundImage 更改图像,但是当我尝试将道具向前移动时它不起作用。

index.js

import { Background } from "./styles";
<Background source={props.backgroundImage} />

样式.js

export const Background = styled.ImageBackground.attrs(
require(props => props.theme.backgroundImage))``;

我的 app.js

import React from "react"; 
import themes from './src/themes'; 
<ThemeProvider theme={themes.light}> 
  <NavigationContainer> 
    <MainStacks /> 
  </NavigationContainer> 
 </ThemeProvider> 

我的 light.js

export default {backgroundImage: "../../../assets/background.png"}

它返回错误Error: Cannot find module 'function (props) { return props.theme.backgroundImage; } '

标签: javascriptreact-nativestyled-componentsreact-props

解决方案


推荐阅读