最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - export global variable defined in react styles js file - Stack Overflow

matteradmin3PV0评论

I have separated styles.js and app.js for a simple app. And I have image resources defined as global constant variables. (For logos and profiles) I tried to add these constant image resources in styles.js as well. But with my current method and syntax, I can't export these constant image resources from styles.js because they are no defined in styles variable wrapper.

styles.js:

 'use strict'

import React, { Component } from 'react';
import {StyleSheet} from 'react-native';

/*
  const background_img= require("../../resource/mybackground.png");
  I tried to define the resource path here
*/

var styles = StyleSheet.create({
   /*Styles Definition*/
});

export default styles

app.js:

'use strict'

import React, { Component } from 'react';
import { AppRegistry, Text, TextInput, View, Image,
TouchableOpacity, Alert} from 'react-native';
import styles from './styles.js';

/*const background= require("../../resource/mybackground.png");*/
// I want to move this part to styles.js


export class LoginScene extends Component {

  constructor(props) {
    super(props);
  }

  render() {
    return (
      <Image
         style={[styles.background, styles.container]}
         source={background} //I want to replace to styles.background_img
         resizeMode="cover"
      ></Image>
      /*Other view definitions*/
    );
  }
}

export default LoginScene

I've indicated where I want to make my change in the above code with ment. If I want to route the source file as what I did above, what should I change in styles.js so that I can export the constant global variables at the same time with styles variable wrapper?

I have separated styles.js and app.js for a simple app. And I have image resources defined as global constant variables. (For logos and profiles) I tried to add these constant image resources in styles.js as well. But with my current method and syntax, I can't export these constant image resources from styles.js because they are no defined in styles variable wrapper.

styles.js:

 'use strict'

import React, { Component } from 'react';
import {StyleSheet} from 'react-native';

/*
  const background_img= require("../../resource/mybackground.png");
  I tried to define the resource path here
*/

var styles = StyleSheet.create({
   /*Styles Definition*/
});

export default styles

app.js:

'use strict'

import React, { Component } from 'react';
import { AppRegistry, Text, TextInput, View, Image,
TouchableOpacity, Alert} from 'react-native';
import styles from './styles.js';

/*const background= require("../../resource/mybackground.png");*/
// I want to move this part to styles.js


export class LoginScene extends Component {

  constructor(props) {
    super(props);
  }

  render() {
    return (
      <Image
         style={[styles.background, styles.container]}
         source={background} //I want to replace to styles.background_img
         resizeMode="cover"
      ></Image>
      /*Other view definitions*/
    );
  }
}

export default LoginScene

I've indicated where I want to make my change in the above code with ment. If I want to route the source file as what I did above, what should I change in styles.js so that I can export the constant global variables at the same time with styles variable wrapper?

Share asked May 28, 2017 at 0:14 RandomEliRandomEli 1,5575 gold badges31 silver badges55 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You can export styles as default, then you can also export another variable which can contain a value or an object.

import React, { Component } from 'react';
import {StyleSheet} from 'react-native';

export const assets = {
  background: require("../../resource/mybackground.png"),
}

var styles = StyleSheet.create({
   /*Styles Definition*/
});

export default styles;

then you can just import the default and the named exports when needed:

import styles, { assets } from './styles.js';
Post a comment

comment list (0)

  1. No comments so far