0

I have Form.js file in styled-ui folder.

Form.js

// input
export const FormControl = styled.input`
  border-radius: 3px;
  border: 1px solid #b2bec3;
  padding: 0 10px;
  height: 34px;
  font-size: 15px;
  outline: 0;
  width: 100%;
`

And I import the Form.js file in styled-ui/index.js like this.

import * as Form from './Form'
import * as Section from './Section'

export default {
  ...Form,
  ...Section
}

This is my conatiner file where import styled-components

import {
  FormControl
} from '@/styled-ui'

After then When I use this,

<FormControl></FormControl>

I got error that '@/styled-ui' does not contain an export named 'FormControl'. However, When I use like this It works well.

import styled from '@/styled-ui'
`<styled.FormControl></styled.FormControl>`

And I'm using webpack for development and production

{
            test: /\.css$/,
            use: [
              require.resolve('style-loader'),
              {
                loader: require.resolve('css-loader'),
                options: {
                  importLoaders: 1,
                },
              },
              {
                loader: require.resolve('postcss-loader'),
                options: {
                  // Necessary for external CSS imports to work
                  // https://github.com/facebookincubator/create-react-app/issues/2677
                  ident: 'postcss',
                  plugins: () => [
                    require('postcss-flexbugs-fixes'),
                    autoprefixer({
                      browsers: [
                        '>1%',
                        'last 4 versions',
                        'Firefox ESR',
                        'not ie < 9', // React doesn't support IE8 anyway
                      ],
                      flexbox: 'no-2009',
                    }),
                  ],
                },
              },
            ],
          },

I'm so confused now. please tell me how can I fix this problem?

1 Answer 1

1
import * as Form from './Form'

...

export default {
  ...Form,
  ...Section
}

is in your case, the same than writing

import * as Form from './Form'

...

export default {
  FormControl: Form.FormControl,
  ...Section
}

That's why <styled.FormControl> works fine.

What you can probably do in your index.js file is something like

export * from './Form'
Sign up to request clarification or add additional context in comments.

4 Comments

Is it only way to solve this problem? There are many components in my Form.js
I just edited my answer. I think you can use export *
you mean just export like this? export * from './Section' export * from './Form' It doesn't work for me.
sorry, there was typo in my code. your code works well! thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.