我有這個組件:JSX打印文本有條件的財產沒有風格的分量值
<Button type="submit" { invalid ? 'primary': null }>
此組件樣式的組件:
import styled from 'styled-components';
export const Button = styled.button`
font-size: 15px;
padding: 0.25em 1em;
border: solid 1px ${(props) => {
let color;
if (props.primary) {
color = 'red';
} else {
color = '#ffffff';
}
return color;
}};
`;
我得到這個錯誤:
Syntax error: Unexpected token ^invalid, expected ... (64:54)
我只需要發送屬性'主'如果無效是真實的,得到這個:
<Button type="submit" primary/>
我不想寫:
primary = { invalid }
的組件調用這個按鈕是:
import React from 'react';
import { Button } from './layouts/cssstyled';
const getConditionalProps = (props) => {
// fill your `invalid` variable in this function or pass it to it
const myprops = {};
myprops.primary = true ;
myprops.secondary = false;
return myprops;
}
const Form = (props) => {
console.log('form props');
console.log(props);
const { handleSubmit, invalid, pristine, reset, submitting, t } = props;
return (
<form onSubmit={handleSubmit}>
<p>Invalid? {JSON.stringify(invalid)}</p>
<Button type="submit" disabled={submitting} primary={invalid ? "" : null} >
Buton styled component does not work
</Button>
<button primary={invalid ? "" : null}> button native works</button>
<div className="formError">
{pristine}
{invalid && t('form.haserrors')}
</div>
</div>
</form>
);
};
export default reduxForm({
form: 'CustomerForm', // a unique identifier for this form
})(Form);
正確的方法肯定會讓你寫'primary = {invalid}'。有什麼理由不寫嗎? – ZekeDroid
我使用一個只需要'主要'的CSS庫,但爲什麼我不能在jsx中打印文本的技術原因? – DDave
@Dave由於react不是一個打印庫,它是一個呈現爲HTML的虛擬DOM。 – Sulthan