当尝试使用数组作为string.Format()
方法的参数时,我收到以下错误:
FormatException:Index(从零开始)必须大于或等于零且小于参数列表的大小.
代码如下:
place = new int[] { 1, 2, 3, 4};
infoText.text = string.Format("Player1: {0} \n Player2: {1} \n Player3: {2} \n Player4: {3}", place);
Run Code Online (Sandbox Code Playgroud)
Array包含四个值,其中的参数String.Format()
也相同.
是什么导致这个错误?
(这infoText.text
只是一个常规的String对象)
假设我创建了一个这样的自定义组件:
const MyComponent = (props) => (
<div
className={props.name}
id={props.id}>
{props.children}
</div>
)
Run Code Online (Sandbox Code Playgroud)
我需要确保props
包含变量,name
并且id
因为否则我想要做的就是不起作用(现在我知道这个代码无论如何都会起作用,但假设它不会).
React有没有办法要求将某些道具传递给组件?
也许这是显而易见的事情,但我找不到任何有关它的信息.
我正在使用React-Router v4在我的React应用程序中导航。以下是包装在withRouter()
函数中的组件,使它能够在单击时更改路线:
const LogoName = withRouter(({history, props}) => (
<h1
{...props}
onClick={() => {history.push('/')}}>
BandMate
</h1>
));
Run Code Online (Sandbox Code Playgroud)
如您所见,我将传递props
给组件,以便更改组件的类。这里的问题props
是undefined
在<LogoName>
组件中。单击另一个组件时,我需要能够更改此组件的类,如下所示:
<LogoName className={this.state.searchOpen ? "hidden" : ""} />
<div id="search-container">
<SearchIcon
onClick={this.handleClick}
className={this.state.searchOpen ? "active" : ""} />
<SearchBar className={this.state.searchOpen ? "active" : ""}/>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我处理点击的方法。基本上只是设置状态。
constructor(){
super();
this.state = {
searchOpen: false
}
}
handleClick = () => {
this.setState( {searchOpen: !this.state.searchOpen} );
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让我传递props
给包装在withRouter()
函数内部的组件,或者有类似的方法来创建一个可以使用React-Router导航并仍然接收道具的组件?
提前致谢。
我正在制作一个多折线图,并且我已经实现了一个画笔,以便能够放大 x 轴上的特定域。但是,当我放大时,我希望 y 轴缩放,以便它的域从 开始[0, maxY]
,其中maxY
是 x 轴上当前选择的最大 y 值。生成我正在使用的线条d3.line()
(它具有 x 和 y 值之间的连接)。这是我目前计算maxY
值的方式:
//Find max and min values in data to set the domain of the y-axis
var maxArray = updatedData.map(function(variable){
//First map the values in the array to a new array
var valuesArray = variable.values.map(function(d){
return d.value;
})
//Find max value in array
return Math.max(...valuesArray);
});
var maxY = Math.max(...maxArray);
Run Code Online (Sandbox Code Playgroud)
这是我设置比例并创建的地方d3.line()
:
var xScale = d3.scaleTime()
.range([0, chartWidth]); …
Run Code Online (Sandbox Code Playgroud) 我有一个div,它应该只在悬停时出现.但是当我加载页面时动画也出现了.我希望隐藏动画div,直到我将鼠标悬停在另一个div上.有没有办法阻止加载页面时动画运行?
这是CSS:
#dim {
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
background: rgba(200, 200, 200, 0.8);
z-index:100;
padding: 5%;
animation: fadeout 1s;
animation-fill-mode: forwards;
}
#dim:hover{
animation: fadein 1s;
animation-fill-mode: forwards;
}
/* Animations */
@keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes fadeout {
from { opacity: 1; }
to { opacity: 0; }
}
Run Code Online (Sandbox Code Playgroud)
这是一个CodePen:https://codepen.io/Martin36/pen/gWwmZO
是有什么区别{x:Static}
和{StaticResource}
WPF 的 XAML 代码中?
例如:
<StackPanel IsEnabled="{Binding Model.IsReadOnly, Converter={StaticResource BoolInverseConverter}}">
Run Code Online (Sandbox Code Playgroud)
和
<StackPanel IsEnabled="{Binding Model.IsReadOnly, Converter={x:Static BoolInverseConverter}}">
Run Code Online (Sandbox Code Playgroud)
我什么时候应该使用其中一个?