我正在尝试向我的应用程序添加一个下拉菜单,然后我选择了Selectize(https://github.com/furqanZafar/react-selectize)。但它似乎无法在页面上正确显示。 This is what is looks like,似乎缺少css样式。我是React的新秀,所以如果这不是一个聪明的问题,请耐心等待。提前谢谢!
webpack.config.js
var path = require("path")
var webpack = require('webpack')
var BundleTracker = require('webpack-bundle-tracker')
module.exports = {
context: __dirname,
entry: './assets/js/index',
output: {
path: path.resolve('./assets/bundles/'),
filename: "[name]-[hash].js",
},
plugins: [
new BundleTracker({filename: './webpack-stats.json'}),
],
module: {
loaders: [
{test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader', query: { presets:['react'] }},
{test: /\.css$/, loader: 'style-loader'},
{test: /\.css$/, loader: 'css-loader', query: { modules: true, localIdentName: '[name]__[local]'}},
],
},
resolve: {
modulesDirectories: ['node_modules', 'bower_components'],
extensions: ['', '.js', '.jsx']
},
}
Dropdown.js
import {ReactSelectize, SimpleSelect, MultiSelect} from 'react-selectize';
import React from 'react';
import 'react-selectize/themes/index.css'
export default class Dropdown extends React.Component {
render() {
return (
<SimpleSelect placeholder="Select a fruit" onValueChange={value => alert(value)}>
<option value = "apple">apple</option>
<option value = "mango">mango</option>
<option value = "orange">orange</option>
<option value = "banana">banana</option>
</SimpleSelect>
)
}
}
index.js
import Dropdown from "./components/dropdown"
var React = require('react')
var ReactDOM = require('react-dom')
ReactDOM.render(<Dropdown />, document.getElementById('container'))
控制台中出现以下错误:
Uncaught Error: addComponentAsRefTo(...): Only a ReactOwner can have
refs. You might be adding a ref to a component that was not created
inside a component's `render` method, or you have multiple copies of
React loaded
答案 0 :(得分:0)
对于许多反应组件,相关的CSS包含在npm模块中,但不能通过import
加载,因为它们不是JS。通常必须单独加载样式。
文档说:
要包含默认样式,请将以下import语句添加到&gt;手写笔文件:@import'node_modules / react-selectize / themes / index.css'
你包含了吗?如果你没有使用Stylus,你可以抓住css并将其粘贴到主CSS文件中。